【问题标题】:HttpRequest with Certificate fails in Azure Web-role带有证书的 HttpRequest 在 Azure Web 角色中失败
【发布时间】:2014-06-09 16:12:39
【问题描述】:

在我部署的 azure web-role 上,我尝试将请求 (GET) 发送到通过请求客户端提供的证书授权请求的 Web 服务器。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

var filepath = Path.GetTempPath();
string certpath = Path.Combine(filepath, "somecert.cer");

Trc.Information(string.Format("Certificate at {0} will be used", certpath));

X509Certificate cert = X509Certificate.CreateFromCertFile(certpath);

WebRequest request = WebRequest.Create(endPoint);
((HttpWebRequest)request).ProtocolVersion = HttpVersion.Version10;
((HttpWebRequest)request).IfModifiedSince = DateTime.Now;

((HttpWebRequest)request).AutomaticDecompression = DecompressionMethods.Deflate |  DecompressionMethods.GZip;

((HttpWebRequest)request).ClientCertificates.Add(cert);

上述代码在 azure-emulator 中完美运行,但在部署时却不行。然后对GetResponse 的调用总是失败。

System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
     at System.Net.HttpWebRequest.GetResponse()
     at XYZ.Import.DataImport.OpenResponseStream(String endPoint)

我阅读了许多现有的讨论线程,其中使用 SecurityProtocolType.Ssl3 解决了问题,但在我的情况下并没有。考虑到它在 azure 上运行,是否有进一步的调试选项?

更新1

我尝试了 Alexey 建议的所有调试步骤。它们确实很有帮助,但很难在 azure 上正确执行。

这是我在至少两个小时后得出的结论。

我使用了这篇文章 [1] 提供的 System.Net 设置。

起初,预期文件夹中不存在输出。需要调整文件夹上的文件系统设置。因此,目标文件夹应允许 NT AUTHORITY\NETWORK SERVICE 帐户。

之后该文件未按预期显示,因为仅提供 app.config 时似乎存在问题。请参阅此线程 [2]。所以我提供了一个 app.config 一个 [ProjectAssembly].dll.config 和一个 web.config,其中包含来自帖子 [1] 的内容。

为了测试问题是否与用户权限有关,我使用提升的权限进行了测试,但没有在帖子 [3] 中显示。

我事先将测试项目更改为以两种模式执行。第一种模式尝试加载 *.cer 文件中的公共部分,如上面的代码所示。 其他版本使用通过该命令加载的私有证书

X509Certificate cert = new X509Certificate2(certpath, "MYPASSWORD",  X509KeyStorageFlags.MachineKeySet); 

因此,我获得了以下见解。

  1. 使用公共部分 (.cer) 时,它仅在权限提升且私有证书导入机器存储时才有效
  2. 使用私有 (.pfx) 时,只有在将私有证书导入机器存储时才有效
  3. 即使没有提升权限,使用 (.pfx) 的第二个设置也可以运行

在调试 CAPI2 日志时,只有没有直接相关性的信息。上述第一点的 System.Net 诊断包含此内容。

System.Net Information: 0 : [1756] SecureChannel#50346327 - Cannot find the certificate in either the LocalMachine store or the CurrentUser store.
[snip]
System.Net Error: 0 : [1756] Exception in HttpWebRequest#36963566:: - The request was aborted: Could not create SSL/TLS secure channel..
System.Net Error: 0 : [1756] Exception in HttpWebRequest#36963566::GetResponse - The request was aborted: Could not create SSL/TLS secure channel..

从这个输出和使用提升权限时的变化情况,我推断我应该结合证书存储进一步研究正在运行的网络角色的权限。

[1]http://msdn.microsoft.com/de-de/library/ty48b824(v=vs.110).aspx

[2]Combined Azure web role and worker role project not seeing app.config when deployed

[3]http://blogs.msdn.com/b/farida/archive/2012/05/01/run-the-azure-worker-role-in-elevated-mode-to-register-httplistener.aspx

【问题讨论】:

  • 当心SSL 3 deprecation。我不知道它是否与您的问题有关,但如果可以,最好切换到 TLS 1.2。

标签: azure httpwebrequest ssl-certificate x509certificate azure-web-roles


【解决方案1】:
  1. 删除 SecurityProtocolType.Ssl3
  2. 打开 CAPI2 日志并检查错误(在您的本地计算机上)。
  3. 如果没有错误,则检查 CA 和中间证书的位置。
  4. 打开 system.net 诊断并检查此日志是否有错误。

this article 中描述了如何查找和打开 CAPI2 事件日志。

希望对您有所帮助。

【讨论】:

  • 嗨,Alexey,感谢您的回答。如果我获得进一步的见解,我会尝试这些步骤并在此处发布结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 2014-09-29
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多