在 win 2019 上执行创建 mTLS 连接的 .net core 自包含应用程序会返回无法联系本地安全机构

问题描述 投票:0回答:1

我正在 Windows 2019 上运行一个自包含的 dotnet 应用程序,以使用带有客户端证书的 mTLS 在远程计算机上执行简单的 httpclient get。

我通过传递 p12 密钥库文件路径在应用程序中加载客户端证书,其中包含证书密钥对和链。

在 Windows 2019 服务器上使用 invoke-restmethod 在 powershell 上执行此 get 可以正常工作,这意味着证书正确加载,并且通过访问证书存储来验证服务器证书。

此外,在本地运行应用程序也可以!这意味着客户端和服务器证书和链都是有效的,并且我的 dotnet 框架可以访问本地 Windows 存储。

这是导致问题的简单调用:

 var certificate = new X509Certificate2(filePath, password);

 Console.WriteLine($"Certificate found in keystore: {certificate.FriendlyName}. {certificate.Thumbprint}. {certificate.Subject}.");


 var handler = new HttpClientHandler();
 var httpClient = new HttpClient(handler)
 handler.ClientCertificates.Add(certificate);

 var result = httpClient.GetAsync("https://urltoserverwithvalidmTLS").GetAwaiter().GetResult();

我得到的例外是:

Exception: System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception
 ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
 ---> System.ComponentModel.Win32Exception (0x80090304): The Local Security Authority cannot be contacted
   --- End of inner exception stack trace ---
   at System.Net.Security.SslStream.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, ExceptionDispatchInfo exception)

应用程序已发布:

--配置发布-r win-x64 --self-contained true

csproj 针对 netcoreapp3.1 框架

此时我不知道为什么这不起作用。任何帮助将不胜感激。

.net-core x509certificate2 windows-server-2019 mtls
1个回答
1
投票

这与与 ssl 协议相关的两个问题的组合有关。在 Windows 2019 上,Dotnet httpclient 默认使用无效的 TLS 协议。 将 SslProtocols 设置为 Tls12 解决了这个问题。

另一个问题是 Windows 2019 在创建 tls 连接时不支持临时密钥。

将连接标志设置为 X509KeyStorageFlags.PersistKeySet 解决了这个问题。 这是一个在 Windows 2019 上使用 .net6 运行的示例 http 客户端:

            var certificate = new X509Certificate2(filePath, password, X509KeyStorageFlags.PersistKeySet);
            var handler = new HttpClientHandler();
            handler.SslProtocols = SslProtocols.Tls12;
            handler.ClientCertificates.Add(certificate);

            var httpClient = new HttpClient(handler);
var result = await httpClient.GetAsync(https://pathToYourSecureUrl);
© www.soinside.com 2019 - 2024. All rights reserved.