与Azure Cloud Blob存储的连接失败,证书无效

时间:2015-07-29 12:52:35

标签: c# .net security azure

我使用Azure存储客户端库连接到我的azure blob存储并发布一些文件。以下代码是我用于建立连接和创建blob容器的摘录:

var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("settingsName"));

client = storageAccount.CreateCloudBlobClient();

var container = client.GetContainerReference("containerName");
            container.CreateIfNotExists();

这在我的本地计算机上运行正常。但是,如果我尝试在不同的服务器上运行完全相同的代码,我将获得以下异常:

Microsoft.WindowsAzure.Storage.StorageException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
   at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
   at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.ConnectStream.WriteHeaders(Boolean async)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)
   --- End of inner exception stack trace ---
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)
   at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExists(BlobContainerPublicAccessType accessType, BlobRequestOptions requestOptions, OperationContext operationContext)

有人知道可能导致这种情况的原因吗?我现在一直在谷歌上搜索这个问题,没有任何解决方案。

1 个答案:

答案 0 :(得分:1)

这个帖子已经休眠了一段时间,但我最近碰到了同样的问题;我想我会分享我的结果,希望将来其他人可以从中受益。

我们在尝试从服务器写入Azure存储blob时收到相同的错误,尽管相同的代码在本地和其他服务器上运行良好:

  

Microsoft.WindowsAzure.Storage.StorageException:基础连接已关闭:无法为SSL / TLS安全通道建立信任关系。 ---> System.Net.WebException:底层连接已关闭:无法为SSL / TLS安全通道建立信任关系。

首先,我们验证了我们在计算机上安装了正确的证书颁发机构。通常,这可能是罪魁祸首。但是,在我们的案例中,我们拥有所有必要的证书颁发机构。

最终出现的问题是服务器无法解析存储帐户的正确IP地址(yourstorageaccountname.blob.core.windows.net)。

在本地,尝试从命令提示符处ping存储帐户,如下所示:

  

ping yourstorageaccountname.blob.core.windows.net

您应该看到本地计算机上的ping能够解析您的blob帐户的IP地址(13.88.144.248等)。

现在尝试从您的服务器ping blob帐户,您将获得此异常。如果ping无法解析IP地址,或者解析了错误的IP地址(就像我的情况一样),您可能已经找到了问题。

如果是这种情况,将简单映射添加到C:\ Windows \ System32 \ drivers \ etc \ hosts文件修复了我们的问题。如果您遇到同样的问题,请在文件中添加IP映射来修复它。映射应将本地ping中的IP映射到blob帐户的主机名。这是一个例子:

  

13.88.144.248 yourstorageaccountname.blob.core.windows.net

请注意,您必须以管理员身份运行文本编辑器(记事本等)才能正确保存主机文件。