Azure Blob 存储生成无效的共享访问签名

Azure Blob Storage Generates Invalid Shared Access Signature

我正在使用以下代码示例为存储在 Azure Blob 存储中的私有文件创建共享访问签名。生成的 Url 有时有效,但大多数时候由于身份验证错误而无效。

public async Task<string> GetFileUrl(string containerName, string fileName)
{    
     ICloudBlob blob = await GetBlob(containerName, fileName);

     string signature = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
     {
           SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5),
           SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7),
           Permissions = SharedAccessBlobPermissions.Read
     });

     return blob.Uri.AbsoluteUri + signature;
}

当形成的 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:</Message>
    <AuthenticationErrorDetail>Signature fields not well formed.</AuthenticationErrorDetail>
</Error>

以下是未通过身份验证的 URL 的参数

sv=2016-05-31
sig=SbVzxo+dv28FvJP+Z9b1w6uvWUT72Pw1BSe3MIRooM0=
st=2017-03-30T11:52:35Z
se=2017-04-06T11:57:35Z
sr=b
sp=r

我发现了问题。 我没有提到问题,但是生成 SAS 的代码是通过 HTTP 客户端调用的,用于使用 SAS 访问 Blob 的 URL 位于响应的位置 header 中。

我试图用

检索 URL
response.Headers.Location.ToString();

但是,我应该使用

response.Headers.Location.OriginalString.ToString();

根据 Microsoft Location.ToString() returns URI 的规范未转义形式。这就是为什么我的签名无效并且里面有+这样的字符。