Azure 存储 SAS 无法进行身份验证

Azure storage SAS failed to authenticate

您好,我正在尝试为我在 Azure 上的文件创建 SAS 令牌。我收到“服务器无法验证请求。确保授权 header 的值正确形成,包括签名”错误。以下是我的代码。

    public async Task<IActionResult> TestBlobClient()
{
    try
    {
        string storageAccount = "myaccount";
        string password = "mykey";

        var sharedKeyCredential = new StorageSharedKeyCredential(storageAccount, password);

        var shareClient = new ShareClient(new Uri("https://aaa.file.core.windows.net/zttsox20201027154501"), sharedKeyCredential);

        ShareDirectoryClient directory = shareClient.GetDirectoryClient("Output/14");
        ShareFileClient file = directory.GetFileClient("637655759841727494_main.wav");

        var shareSasBuilder = new ShareSasBuilder
        {
            ShareName = "aaa",
            Protocol = SasProtocol.None,
            ExpiresOn = DateTime.UtcNow.AddYears(50),
            Resource = "b"
        };

        shareSasBuilder.SetPermissions(ShareFileSasPermissions.Read);
        var url = new Uri(file.Uri + "?" + shareSasBuilder.ToSasQueryParameters(sharedKeyCredential).ToString());

        return Ok(url);
    }
    catch (Exception ex)
    {

        return StatusCode(StatusCodes.Status400BadRequest, ex.Message);
    }

问题出在 Resource = "b"。当您将资源设置为 b 时,这意味着您正在获取一个 blob 的 SAS 令牌。考虑到您正在获取文件共享的 SAS 令牌,此参数的值应为 s.

请尝试类似的操作:

        var shareSasBuilder = new ShareSasBuilder
        {
            ShareName = "zttsox20201027154501",
            Protocol = SasProtocol.None,
            ExpiresOn = DateTime.UtcNow.AddYears(50),
            Resource = "s"
        };

更多详情,请看这个link:https://docs.microsoft.com/en-us/dotnet/api/azure.storage.sas.sharesasbuilder.resource?view=azure-dotnet#Azure_Storage_Sas_ShareSasBuilder_Resource