【问题标题】:SAS for Blob write access keeps causing 403 authentication error with azure storage用于 Blob 写入访问的 SAS 不断导致 Azure 存储出现 403 身份验证错误
【发布时间】:2013-09-01 06:16:38
【问题描述】:

当我尝试使用使用天蓝色客户端存储库生成的 SAS 密钥时,我不断收到来自 Azure 存储服务的 403 身份验证错误。调用代码正在抛出异常。

这是我得到的例外:

StatusMessage:Server failed to authenticate the request. Make sure the value of
Authorization header is formed correctly including the signature.
ErrorCode:AuthenticationFailed

这是正在生成的 SAS 密钥(最后的签名不准确,因为我不想分享):

https://evinsight.blob.core.windows.net/jimsworld/jellyfish2.png?sv=2012-02-12&se=2013-08-29T03%3A36%3A52Z&sr=b&sp=w&sig=FJALKJFLKASJDF%JLKSDJLK%LJDLFKSDFJKL

这里是调用代码:

/// Create the document and file objects and then return the fileAccessToken
/// <param name = "file_name"></param> */
public void GetSasInfoFromEb(string file_name)
{
    /*********************************** EVINSIGHT TESTING CODE ********************************************/
    try
    {
        AzureStorageTester ast = new AzureStorageTester();
        BlobSasUri = ast.getStorageLibrarySas(file_name);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    /***************************************************************************************************************/
}

/// Sends the whole small file to Azure Blob
public void WriteSmallFileToBlob()
{
    int fileId = 0;
    try
    {
        Blob = new CloudBlockBlob(new Uri(BlobSasUri));
        Blob.UploadFromStream(File.InputStream);


        File.InputStream.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

这是 SAS 生成代码:

    /// <summary>
    ///  Returns the URI similar to how eB does it
    /// </summary>
    /// <param name="blobName">The name of the file being uploaded/downloaded</param>
    /// <returns>The full uri for blob access</returns>
    public string getStorageLibrarySas(string blobName)
    {
        string sasKey;
        string uri;

        setupBlobContainer();

        blobName = blobName.ToLower();
        _blob = _cloudRootContainer.GetBlockBlobReference(blobName);


        sasKey = _blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
        {
            Permissions = SharedAccessBlobPermissions.Write,
            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1)
        });

        return string.Format(CultureInfo.InvariantCulture, "{0}{1}", _blob.Uri, sasKey);
    }

    /// Creates the Blob Container
    private void setupBlobContainer()
    {
        try
        {
            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials credentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(_accountName, _accountKey);

            // Create the storage account with the connection string
            Microsoft.WindowsAzure.Storage.CloudStorageAccount _cloudStorageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(credentials, true);

            _cloudBlobClient = _cloudStorageAccount.CreateCloudBlobClient();
            _cloudRootContainer = _cloudBlobClient.GetContainerReference(_rootPath.ToLower());
            _cloudRootContainer.CreateIfNotExists();

            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();


            containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off;
            _cloudRootContainer.SetPermissions(containerPermissions);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

有什么想法吗?欣赏!

【问题讨论】:

  • 你从哪里得到异常?在上面的代码中,还是在尝试使用您的 SAS URI 之一的调用应用程序中?如果是后者,我可以很好地猜测它与时钟漂移有关。如果是前者,请指出异常发生的位置。
  • 尝试使用 SAS 的调用代码抛出异常。服务器基本上拒绝 SAS 密钥。我漏掉了开始时间,那不应该解决时钟偏差问题吗?
  • 你能通过 Fiddler 追踪请求/响应吗?您应该通过这种方式获得有关 403 错误的更多信息。

标签: .net azure key storage blob


【解决方案1】:

您的代码在我看来非常完美。事实上,我使用您的代码创建了一个示例应用程序,一切正常。

您能否检查一下 SAS 令牌的 sig 部分?尤其是在那里寻找 + 签名。不久前,我在这里看到了一个类似的问题,SAS URL 偶尔会因 403 错误而失败。原来 SAS 令牌中有一个加号,它是 space 的 URL 编码值。由于 SAS 令牌是作为 URL 发送的,因此令牌中的+ 符号被解释为空格并导致 403 错误。

【讨论】:

    【解决方案2】:

    【讨论】:

      【解决方案3】:

      每个 cmets,让我将其发布为建议的答案:您可能看到时钟偏差。尝试将 SAS 开始时间设置为比当前时间早一分钟。默认情况下,开始时间设置为(生成 SAS 的服务器的)当前开始时间。时间设置可能与存储系统的时钟略有偏差。

      【讨论】:

      • 我尝试将开始时间设置为 -10,但仍然收到 403 身份验证错误。
      • 这是我得到的异常:StatusMessage:Server 未能验证请求。确保 Authorization 标头的值正确形成,包括签名。错误代码:身份验证失败
      【解决方案4】:

      事实证明,我的问题集中在与 CLIENT 创建 CONTAINER 上。有两个不同的 Azure 存储库(storage 和 StorageClient)。我使用代码进行存储,但使用旧的 StorageClient 方法,在该方法中,您将完整路径传递到客户端对象“getref ...”方法中以获取容器。事实证明,2.0 存储库只需要传入容器 NAME 即可获取容器。这两个不同的库,所有东西都用相同的命名,确实让事情变得混乱。

      【讨论】:

        猜你喜欢
        • 2019-10-23
        • 2022-07-05
        • 2021-09-24
        • 1970-01-01
        • 2017-10-30
        • 2017-12-07
        • 2021-12-17
        • 2023-02-24
        • 1970-01-01
        相关资源
        最近更新 更多