【问题标题】:Connection to Azure Cloud Blob Storage fails with invalid certificate连接到 Azure 云 Blob 存储失败,证书无效
【发布时间】:2021-10-12 20:01:23
【问题描述】:

我正在使用 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)

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

【问题讨论】:

  • 您可以分享您的存储帐户的连接设置吗?请删除或屏蔽帐户密钥等敏感信息。
  • 我的 appSettings 如下: 它适用于我的本地机器和服务器上完全一样。
  • azuresecurity.codeplex.com/… 您也可以尝试将 azure 证书添加为可信证书。
  • 我了解验证过程中出现问题,并且证书似乎无效。但我什至不确定它在说什么证书。
  • 您确定在本地运行时没有使用开发存储吗?

标签: c# .net security azure


【解决方案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 映射添加到文件应该可以解决它。映射应将 IP 从本地 ping 映射到 blob 帐户的主机名。这是一个例子:

13.88.144.248 yourstorageaccountname.blob.core.windows.net

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

【讨论】:

    【解决方案2】:

    我刚刚在为DateTime.UtcNow 创建填充程序的单元测试期间访问表存储时遇到此错误消息。

    此代码至少从 2017 年开始使用,因此必须是最近的包更新导致此问题(使用 .NET Framework 4.8)。

    【讨论】:

      猜你喜欢
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 2020-05-28
      • 2021-07-11
      • 2017-09-17
      • 1970-01-01
      • 2022-12-11
      相关资源
      最近更新 更多