c# - Azure Blob 存储 Sas 错误 : AuthenticationFailed

标签 c# azure azure-blob-storage sas-token

我正在尝试在 Azure Blob 存储中使用 SAS token ,如下 this tutorial ,但我遇到了这个错误:

<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:2aada4ff-901e-0011-116c-8bc84f000000 Time:2019-10-25T19:41:37.0381744Z
  </Message>
  <AuthenticationErrorDetail>
    Signature did not match. String to sign used was r 2019-10-25T19:26:51Z 2019-10-25T20:31:51Z /blob/platinepersistencesg/$root/documents-legal-entity-01d6d631-bc1e-54e7-894e-f67297a2bae7 2019-02-02 b
  </AuthenticationErrorDetail>
</Error>

这是我的代码:

public async Task<IActionResult> GetSasToken()
{
    const string containerName = "documents-legal-entity-01d6d631-bc1e-54e7-894e-f67297a2bae7";
    const string blobName = "09578f41-e7fb-4765-bf41-869ea649f03a.pdf";
    const SharedAccessBlobPermissions permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Create;

    var blobClient = CloudStorageAccount
        .Parse("DefaultEndpointsProtocol=https;AccountName=<account_name>;AccountKey=<account_key>;EndpointSuffix=core.windows.net")
        .CreateCloudBlobClient();

    var container = blobClient.GetContainerReference(containerName);
    var blob = container.GetBlockBlobReference(blobName);
    var policy = new SharedAccessBlobPolicy
    {
        SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
        Permissions = permissions
    };
    var sasToken = blob.GetSharedAccessSignature(policy);
    var sasUri = container.Uri + sasToken;

    return Ok(new { uri = sasUri });
}

我能够按照 this answer 使其工作,但为了简单起见,我想改用 Azure 客户端,并避免携带存储 key 。

最佳答案

您唯一缺少的是 URL 构造中的 blob 名称。只需要更改:

var sasUri = 容器.Uri + sasToken;

...到...

var sasUri = blob.Uri + sasToken;

关于c# - Azure Blob 存储 Sas 错误 : AuthenticationFailed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58565283/

相关文章:

spring-boot - Kotlin Springboot 应用程序无法找到 Azure Application Insight 的连接字符串

Azure CDN 规则引擎重写默认文档并删除 .html 扩展名

c# - EF 上的数据库连接错误

C# Regex 替换样式 HEIGHT 标签

python - 适用于 Python 的 Azure 网站管理客户端 : how to get web app by ID?

java - Azure Cosmos Mongo API 版本 3.6,但无法连接 mongo.cosmos.azure.com :443

azure - 是否可以通过 FTP 访问 Azure Blob 存储?

node.js - npm azure-storage 下载文件为 base64

c# - 以编程方式隐藏重复表

c# - 使用 LINQ 创建 SelectListItem 的集合