c# - System.Net.WebException:请求被中止:无法创建 SSL/TLS 安全通道 - dot net framework 4.7.1
问题描述
我收到以下错误,我已验证证书已正确安装,指纹很好。下面是我的代码。
On Running below command from Powershell :
[Net.ServicePointManager]::SecurityProtocol it shows as SSL3, TLS
我正在使用点网框架 4.7.1。
System.Net.WebException:请求被中止:无法创建 SSL/TLS 安全通道。
web.config :
<httpRuntime requestValidationType="SARQA.NET.BaseCode.NoValidationRequestValidator"
maxRequestLength="4096" executionTimeout="180" targetFramework="4.7.1" />
public static WebRequestHandler getHandlerWithClientCert(string thumbprint)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 |
SecurityProtocolType.Tls12;
var wrhandler = new WebRequestHandler
{
AutomaticDecompression = System.Net.DecompressionMethods.GZip |
System.Net.DecompressionMethods.Deflate
};
wrhandler.ClientCertificates.Add(GetCertFromStore(thumbprint));
return wrhandler;
}
public static X509Certificate2 GetCertFromStore(string thumbprint)
{
X509Certificate2 cert = null;
//removing unwanted char from hexadecimal thumbprint
thumbprint = Regex.Replace(thumbprint, @"[^\da-fA-F]", string.Empty).ToUpper();
#if(DEBUG)
var store = new X509Store(StoreName.Root,StoreLocation.CurrentUser);
#else
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
#endif
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection = store.Certificates.Find(
X509FindType.FindByThumbprint, thumbprint, true
);
if (certCollection.Count > 0)
{
cert = certCollection[0];
}
store.Close();
return cert;
}
解决方案
推荐阅读
- r - 从 data.table 中删除所有名称包括“问题”的列都是 NA 的行
- python - 导入包时出现Python导入错误
- laravel - 将 Laravel 应用程序(使用 Voyager 和 Voyager 前端创建)部署到 Google Cloud Compute Engine、Ubuntu 18.04 时出现 404 错误
- image - 如何使用 Julia 显示图像
- reactjs - 使用路由器反应 404 页面
- c++ - 用于插入的指针包装器
- bash - 如果在列中找到模式,则将特定值添加到 bash 中的下一列
- r - 如何修复 osmdata 包中的“找不到节点”
- angular - 将值从一个组件发送到另一个组件时出现服务问题
- android - 我们能否获取路线 TBT 信息的语音列表(语音指导文本),以便我可以自己说出这些值