You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Aspire测试在Azure DevOps流水线中抛出SSL异常的解决方法

解决Aspire测试在Azure DevOps自托管Windows代理上的SSL证书信任问题

问题背景

创建默认Aspire启动解决方案后,测试代码在本地运行正常,但部署到Azure DevOps自托管Windows代理时,测试抛出System.Security.Authentication.AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot异常,已执行dotnet dev-certs https --clean和dotnet dev-certs https --trust命令但问题未解决,环境为运行Aspire workload 8.0.2的自托管Windows代理。

解决方案

1. 为代理服务账户导入并信任开发证书

dotnet dev-certs https --trust仅对当前登录用户生效,而Azure DevOps自托管代理通常以本地系统账户或专用服务账户运行,需将证书导入到代理账户的信任存储区:

  • 首先导出当前用户的HTTPS开发证书:
    dotnet dev-certs https -ep "$env:USERPROFILE\.aspnet\https\aspnetcore-dev-cert.pfx" -p "自定义密码"
    
  • 然后将证书导入到本地计算机的根证书存储区(确保代理账户能访问):
    $certPath = "$env:USERPROFILE\.aspnet\https\aspnetcore-dev-cert.pfx"
    $password = ConvertTo-SecureString "自定义密码" -AsPlainText -Force
    Import-PfxCertificate -FilePath $certPath -Password $password -CertStoreLocation "Cert:\LocalMachine\Root"
    
    若代理使用本地系统账户运行,必须导入到LocalMachine\Root而非当前用户存储区。

2. 测试环境临时禁用SSL验证(仅用于测试)

如果仅需让测试通过,可在创建HttpClient时跳过SSL证书验证:
修改测试代码中的CreateHttpClient逻辑:

var client = app.CreateHttpClient("gateway", clientBuilder =>
{
    clientBuilder.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
    {
        ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
    });
});

⚠️ 此方法仅适用于测试环境,生产环境禁止使用。

3. 配置Aspire使用自定义可信证书

在AppHost项目中指定自定义证书,替代自动生成的开发证书:
在Program.cs的服务配置中添加证书参数:

builder.AddGateway("gateway")
       .WithHttps()
       .WithCertificateFromStore("证书指纹", StoreName.My, StoreLocation.LocalMachine);

随后在自托管代理机器上安装该自定义证书到对应存储区,并确保代理账户拥有证书访问权限。

4. 检查代理机器的证书存储

  • 打开certlm.msc(本地计算机证书管理器),查看受信任的根证书颁发机构中是否存在Aspire生成的开发证书,若缺失则手动导入。
  • 确认证书未过期,且证书主体名称与Aspire服务使用的域名(如localhost)匹配。

内容的提问来源于stack exchange,提问作者Mike-E

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 22:59:54