简体   繁体   English

无法创建SSL / TLS安全通道 - 作为exe工作但不在asp.net mvc中工作

[英]Could not create SSL/TLS secure channel - works as exe but not in asp.net mvc

I have a problem with connectiong to an api over https. 通过https连接api时遇到问题。 I wrote a little console application: 我写了一个小控制台应用程序:

var handler = new WebRequestHandler();

X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

store.Open(OpenFlags.ReadOnly);
foreach (X509Certificate2 mCert in store.Certificates)
{    
    // add my locale certificate
    if (mCert.FriendlyName == "some_identifier_name")
        handler.ClientCertificates.Add(mCert);
}

var httpClient = new HttpClient(handler);
var response= httpClient.GetAsync("https://url-to-my.com/api/something").Result;
var result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);        

This little script works fine, I get a statuscode 200 and also the json data from the api. 这个小脚本工作正常,我得到一个状态代码200和来自api的json数据。 As you can see I have a certificate installed on my local machine, which I add in the code. 正如您所看到的,我在本地计算机上安装了证书,我在代码中添加了证书。

I tried the same script on my webserver and everything works fine there too! 我在我的网络服务器上尝试了相同的脚本,一切都运行正常! But if I run the script in my mvc application I still get the message "The request was aborted: Could not create SSL/TLS secure channel". 但是,如果我在我的mvc应用程序中运行该脚本,我仍然会收到消息“请求已中止:无法创建SSL / TLS安全通道”。

Where is the difference with the same code if I run it as an exe or as a mvc website? 如果我将它作为exe或mvc网站运行,那么相同代码的区别在哪里?

PS: This code is not the answer to my question, I already tried it: PS:这段代码不是我的问题的答案,我已经尝试过了:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

Solution: IIS > Applications Pools > Chose your Apppool > Advanced Settings > Identity > Change to LocalSystem 解决方案:IIS>应用程序池>选择您的Apppool>高级设置>身份>更改为LocalSystem

Seems like you have to give your application pool enough rights to read the certificate from your local machine. 似乎您必须为应用程序池提供足够的权限来从本地计算机读取证书。

After spending 2 to 3 days for above issue, found following solution and you don't want to change "Identity" to "LocalSystem" (In Apppool Advance setting), 在上述问题花了2到3天后,找到以下解决方案并且您不想将“身份”更改为“LocalSystem”(在Apppool Advance设置中),

  1. run up mmc.exe and choose File->Add/Remove Snap-in, select certificates on the left-hand list and when it asks you, choose Computer Account and Local Computer. 运行mmc.exe并选择File-> Add / Remove Snap-in,在左侧列表中选择证书,当它询问您时,选择Computer Account and Local Computer。 Click OK to exit the selection page. 单击“确定”退出选择页面。
  2. Open up the Personal->Certificates folder under the snap-in you just enabled. 打开刚刚启用的管理单元下的Personal-> Certificates文件夹。 It is possible, that the Certificates folder doesn't exist (if it is empty). Certificates文件夹可能不存在(如果它是空的)。
  3. Right-click in the contents pane of Personal or Certificates and choose All Tasks->Import 右键单击“个人”或“证书”的内容窗格,然后选择“所有任务” - >“导入”
  4. Go through the wizard to import your certificate and select the option to "mark it exportable" which is usually needed for SSL usage (I think it includes the private key only when this is ticked). 通过向导导入您的证书,并选择“标记为可导出”选项,这通常是SSL使用所需的(我认为只有在勾选时才包含私钥)。
  5. With the certificate imported, select it, choose All Tasks->Manage Private Keys 导入证书后,选择它,选择“所有任务” - >“管理私钥”
  6. You will get a familiar security dialog where you can add users who can access the private key. 您将获得一个熟悉的安全对话框,您可以在其中添加可以访问私钥的用户。 Add the account for the user that is running the application pool for your web site. 为您的网站运行应用程序池的用户添加帐户。
  7. If you are using "App Pool Identity", then the users are found with IIS Apppool\\app.pool.name Note that if you are running some versions of Windows Server, you will need to change the "location" parameter to point to the local machine rather than the domain which is selected by default, otherwise the user won't be found. 如果您使用的是“App Pool Identity”,则会找到IIS Apppool \\ app.pool.name的用户请注意,如果您运行的是某些版本的Windows Server,则需要更改“location”参数以指向本地计算机而不是默认选择的域,否则将找不到该用户。

Reference : http://lukieb.blogspot.com/2015/04/the-request-was-aborted-could-not.html 参考: http//lukieb.blogspot.com/2015/04/the-request-was-aborted-could-not.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET,HttpWebRequest和“无法创建SSL / TLS安全通道” - ASP.NET, HttpWebRequest and “Could not create SSL/TLS secure channel” 无法创建SSL / TLS安全通道适用于winforms但不适用于asp.net - Could not create SSL/TLS secure channel works on winforms but not in asp.net .Net无法创建SSL / TLS安全通道 - .Net could not create SSL/TLS secure channel asp.net core 2.0 中的 WCF - 无法为具有权限的 SSL/TLS 安全通道建立信任关系 - WCF in asp.net core 2.0 - Could not establish trust relationship for the SSL/TLS secure channel with authority .NET请求已中止:无法创建SSL / TLS安全通道 - .NET The request was aborted: Could not create SSL/TLS secure channel 无法从 .NET C# 创建 SSL/TLS 安全通道 - Could not create SSL/TLS secure channel from .NET C# 该请求已中止:无法创建SSL / TLS安全通道.Net 4.5.2 - The request was aborted: Could not create SSL/TLS secure channel .Net 4.5.2 无法为Facebook创建SSL / TLS安全通道 - Could not create SSL/TLS secure channel for Facebook BufferNotEnough无法创建SSL / TLS安全通道 - BufferNotEnough Could not create SSL/TLS secure channel 无法创建SSL / TLS安全通道-HttpWebRequest - Could not create SSL/TLS secure channel - HttpWebRequest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM