由于证书无效,连接到Azure云Blob存储失败

本文关键字:Azure Blob 失败 存储 连接 于证书 证书 无效 | 更新日期: 2023-09-27 18:05:16

我正在使用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存储失败

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

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

Microsoft.WindowsAzure.Storage。底层连接已关闭:无法为SSL/TLS安全通道建立信任关系。--> system.net.webeexception:底层连接已关闭:无法为SSL/TLS安全通道建立信任关系。

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

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

在本地,尝试从命令提示符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

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

在为DateTime.UtcNow创建shim的单元测试中,我刚刚遇到了访问表存储的错误消息。

这段代码至少从2017年开始使用,所以最近的包更新肯定导致了这个问题(使用。net Framework 4.8)。