繁体   English   中英

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

[英]Connection to Azure Cloud Blob Storage fails with invalid certificate

我正在使用 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:基础连接已关闭:无法为 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 映射应该可以解决问题。 映射应将 IP 从本地 ping 映射到 Blob 帐户的主机名。 下面是一个例子:

13.88.144.248 yourstorageaccountname.blob.core.windows.net

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

我刚刚在单元测试期间遇到了此错误消息访问表存储,在该测试中我为DateTime.UtcNow创建了一个垫片。

这段代码至少从 2017 年开始使用,所以最近的包更新肯定是造成这种情况的(使用 .NET Framework 4.8)。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM