c# - Azure 共享访问签名 - 签名不匹配

标签 c# azure azure-blob-storage

我收到此错误:

<Error>
<Code>AuthenticationFailed</Code>
<Message>
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:6c3fc9a8-cdf6-4874-a141-10282b709022 Time:2014-07-30T10:48:43.8634735Z
</Message>
<AuthenticationErrorDetail>
Signature did not match. String to sign used was rwl 2014-07-31T04:48:20Z /acoustie/$root 2014-02-14
</AuthenticationErrorDetail>
</Error>

当我生成 sas(共享访问签名)然后将该 sas 粘贴到容器 uri 末尾到浏览器中时,我会得到它。这是生成的 sas 的完整地址:

https://acoustie.blob.core.windows.net/mark?sv=2014-02-14&sr=c&sig=E6w%2B3B8bAXK8Lhvvr62exec5blSxsA62aSWAg7rmX4g%3D&se=2014-07-30T13%3A30%3A14Z&sp=rwl

我已经搜索过SO和Google,并尝试了很多组合,据我所知,我做的一切都是正确的,我知道我不是,我只是看不到它......真的希望有人可以帮助:-\

需要明确的是,我在容器上生成 sas,而不是特定的 blob,也不是在根容器上。对 Blob 的访问定义为公共(public) Blob。我的最终目标是简单地允许使用 sas 写入容器,同时“调试”我已向 SharedAccessBlobPolicy 添加了大部分权限。

我尝试在容器名称的 beginning and ending 处添加\。没有变化。

这是我用来生成 sas 的代码:

    var blobClient = storageAccount.CreateCloudBlobClient();
    //Get a reference to the blob container 
    var container = blobClient.GetContainerReference(containerName);

    // Do not set start time so the sas becomes valid immediately.
    var sasConstraints = new SharedAccessBlobPolicy 
    {
        SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(30), 
        Permissions = SharedAccessBlobPermissions.Write 
        | SharedAccessBlobPermissions.Read
        | SharedAccessBlobPermissions.List,
    };

    var sasContainerToken = container.GetSharedAccessSignature(sasConstraints);

    //Return the URI string for the container, including the SAS token.
        var sas = string.Format("{0}{1}", container.Uri.AbsoluteUri, sasContainerToken);
        Logger.Debug("SAS: {0}", sas);
        return sas;

它生成了一个签名,但它似乎不是一个有效的签名。

我尝试过不同的容器,更改访问策略,有或没有开始时间,将到期时间延长到从现在起超过 12 小时(我位于 UTC+10 时区),这似乎并不重要我更改它会导致相同的“签名不匹配”错误。

我什至尝试过使用旧版本的“WindowsAzure.Storage”,所以我现在尝试了 4.2 和 4.1。即使在不同的浏览器中尝试了 uri,确实应该没有什么区别,但是嘿...

非常感谢任何建议:-)

最佳答案

简短回答:

comp=list&restype=container 添加到您的 SAS URL,您应该不会收到此错误。

长答案:

基本上,从您的 SAS URL 来看,Azure 存储服务无法识别您尝试访问的资源是 Blob 还是容器,并假定它是 Blob。由于它假定资源类型是 blob,因此它使用 $root blob 容器进行 SAS 计算(您可以从错误消息中看到)。由于 SAS 是针对 mark blob 容器计算的,因此您会收到此签名不匹配错误。通过指定 restype=container,您将告诉存储服务将资源视为容器。根据 REST API 规范,需要 comp=list

关于c# - Azure 共享访问签名 - 签名不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25038429/

相关文章:

c# - 使用 C# 追加文本文件

c# - 如何使用 WPF 和 MVVM 添加带有标题和内容的 TabItem

sql-server - 在 Azure DevOps 中构建 SSIS 项目时出现 System.ArgumentException。在本地工作

c# - 通过Azure Function处理多个请求

azure - Cloud Explorer - 无法访问开发 Blob 容器

c# - 在 C# MVC 中,您可以设置每个操作方法在处理其余请求之前必须执行的全局行为吗?

c# - JSON.NET 的 JsonConvert 我做错了什么

amazon-web-services - 有条件地启用或禁用 root main.tf terraform 中的模块

azure - 如何使用 PowerShell 在 Azure 存储容器内创建文件夹结构

azure - 如何添加限制,仅Azure CDN可以访问存储帐户容器中的静态网站。无法直接访问静态网站 blob URL