c# - 连接到 Azure 云 Blob 存储失败,证书无效

标签 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)

有人知道是什么原因造成的吗?我已经在谷歌上搜索这个问题好几天了,没有任何解决方案。

最佳答案

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

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

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.

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

最终的问题是服务器无法解析存储帐户的正确 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

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

关于c# - 连接到 Azure 云 Blob 存储失败,证书无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31700966/

相关文章:

c# - Fluent Nhibernate 保存子实体

database - 审计跟踪和实现 SOX/HIPAA/等,敏感数据的最佳实践

c# - 日期时间格式一年中的第几天

C# 哪个是精度 3 的最佳数据类型?

c# - RestSharp 在上传时将整个文件加载到内存中。如何避免?

c# - 使用 .NET 检测 .ogg 文件属性? channel 数,每 channel 位数,采样率?

javascript - Watin - 填写表格后提交按钮保持禁用状态

security - 如何检查我的 Web 服务器上的 SSL 漏洞?

javascript - 从 JavaScript 使用参数调用 Controller 操作是否安全?

c# - 匹配进程参数的正则表达式