【发布时间】:2022-08-02 19:25:22
【问题描述】:
var httpClientHandler = new HttpClientHandler
{
SslProtocols = SslProtocols.Tls12,
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; },
};
var httpClient = new HttpClient(httpClientHandler)
{
BaseAddress = new Uri(\"https://api.myservice.com:4443\"),
Timeout = TimeSpan.FromSeconds(10),
};
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($\"USER1:PASS1\"));
httpClient.DefaultRequestHeaders.Add(\"Authorization\", $\"Basic {credentials}\");
var result = await httpClient.PostAsync(\"/login\", null);
我的应用程序.csproj
<PropertyGroup Condition=\"\'$(Configuration)\'==\'Release\' And \'$(TargetFramework)\'==\'net5.0-windows\' And \'$(RuntimeIdentifier)\'==\'win-x86\'\">
<OutputPath>..\\..\\release\\</OutputPath>
<AssemblyName>my-app</AssemblyName>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>
- my-app.exe 是带有 .NET 5.0 的 x86 程序集
- .NET 5.0 运行时已安装(x86 和 x64)
- .NET 框架 4.8 已安装
- KB3033929、KB3063858 和 KB3140245 已安装
- https://api.myservice.com:4443 支持 TLS 1.2
它适用于 Win10 x64,但适用于 Win7 Sp1 x64,它会生成:
The SSL connection could not be established, see inner exception.
- 内部异常为空
我已经添加了这些注册表项:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\SecurityProviders\\SCHANNEL\\Protocols\\TLS 1.2\\Client]
\"DisabledByDefault\"=dword:00000000
\"Enabled\"=dword:00000001
[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\.NETFramework\\v4.0.30319]
\"SystemDefaultTlsVersions\"=dword:00000001
\"SchUseStrongCrypto\"=dword:00000001
[HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\.NETFramework\\v4.0.30319]
\"SystemDefaultTlsVersions\"=dword:00000001
\"SchUseStrongCrypto\"=dword:00000001
-
TLS 1.1 已过时,这是您添加到注册表的默认设置。您不需要更改注册表。使用以下是等效的:SslProtocols = SslProtocols.Tls12。以下是不支持 TLS 1.2 加密模式的 Net 4.0 v4.0.30319。您需要使用在操作系统中执行 TLS 的 Net 4.7.2 或更高版本(而不是在 Net 中)。使用记事本打开 csproj 并确保禁用使用 Net for TLS 的选项并使用操作系统(默认)。还要检查 Net 的目标版本。
-
@jdweng 我已经尝试过没有 SslProtocols=SslProtocols.Tls12 并且没有注册表项。可能需要一些windows更新,我已经安装了KB3033929和KB3063858
-
听起来 Win7 中的某些 Windows 更新缺少 TLS。或者Win7上缺少证书。
-
为了诊断这一点,Wireshark 跟踪将非常有用。您是否应用了来自support.microsoft.com/en-us/topic/… 的相关更新?您是否知道 Windows 7 已严重过时,并且不再接收任何安全更新?
-
@Charlieface 我已经应用了 KB3140245,但问题仍然存在。
标签: c# ssl windows-7 tls1.2 dotnet-httpclient