带有证书的HttpRequest在Azure Web角色中失败

时间:2014-06-09 16:12:40

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

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

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上运行,还有进一步的调试选项吗?

UPDATE1

我尝试了Alexey建议的所有调试步骤。它们确实非常有用,但很难在天蓝色上正确执行。

这是至少两个小时后我想出的。

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

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

之后文件没有按预期显示,因为当只提供app.config时似乎存在问题。见这个主题[2]。所以我提供了一个app.config [ProjectAssembly] .dll.config和一个带有帖子[1]内容的web.config。

测试问题是否与我使用提升权限进行测试的用户权限相关,而不是在帖子[3]中显示。

事先我改变了Test-Project以两种模式执行。第一种模式尝试在* .cer文件中加载公共部分,如上面的代码所示。 另一个版本使用使用此命令加载的私有证书

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

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

  1. 使用公共部分(.cer)时,只有在提升权限并将私有证书导入计算机存储区时才能使用
  2. 使用私有(.pfx)时,只有将私有证书导入到计算机存储中才能使用它。
  3. 第二个设置(.pfx)即使没有提升权限也会运行
  4. 调试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..
    

    从这个输出和使用提升权限时的变化情况,我推断出我应该进一步研究正在运行的web-role与证书存储区的权利。

    [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

1 个答案:

答案 0 :(得分:2)

  1. 删除SecurityProtocolType.Ssl3
  2. 打开CAPI2日志并检查错误(在本地计算机上)。
  3. 如果没有错误,请检查CA和中间证书的位置。
  4. 启用system.net诊断并检查此日志是否有错误。
  5. this article中介绍了如何查找和启用CAPI2事件日志。

    希望得到这个帮助。