Azure Blob存储Sas错误:身份验证失败

时间:2019-10-25 20:23:00

标签: c# azure azure-storage-blobs sas-token

我尝试在this tutorial之后在Azure Blob存储中使用SAS令牌,但遇到此错误:

<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客户端。

1 个答案:

答案 0 :(得分:0)

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

var sasUri = container.Uri + sasToken;

...到...

var sasUri = blob.Uri + sasToken;