我试图使用邮递员通过共享密钥读取存储在Microsoft Azure Blob存储中的图像文件,这给了我以下错误。
<?xml version="1.0" encoding="utf-8"?>
<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:48328e8c-601e-000a-3508-bec540000000
Time:2019-02-06T10:43:06.4920228Z</Message>
<AuthenticationErrorDetail>The MAC signature found in the HTTP request 'jn37EV4KPWj3wQANreUQy8ih+H5rFOp0fqj1DebgBMk=' is not the same as any computed signature. Server used following string to sign: 'GET
image/jpeg
x-ms-date:Wed, 06 Feb 2019 10:38:54 GMT
x-ms-version:2018-03-28
/<accountName>/<containerFolder>/<image.jpg>'.</AuthenticationErrorDetail>
</Error>
我用来计算签名的代码是:
class Program
{
private static string storageAccountKey = "<account_key>";
static void Main(string[] args)
{
string utcDate = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
string authStr = "GET\n\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:" + utcDate + "\nx-ms-version:2018-03-28\n/<account_name>/<container_name>/<image.jpeg>";
string hash = CreateAuthorizationHeader(authStr);
Console.WriteLine(hash);
Console.ReadKey(true);
}
public static String CreateAuthorizationHeader(String canonicalizedString)
{
String signature = String.Empty;
using (HMACSHA256 hmacSha256 = new HMACSHA256(Convert.FromBase64String(storageAccountKey)))
{
Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(canonicalizedString);
signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
}
String authorizationHeader = String.Format(
CultureInfo.InvariantCulture,
"{0} {1}:{2}",
AzureStorageConstants.SharedKeyAuthorizationScheme,
AzureStorageConstants.Account,
signature
);
return authorizationHeader;
}
}
class AzureStorageConstants
{
public static string SharedKeyAuthorizationScheme = "SharedKey";
public static string Account = "<account_name>";
}
我要传递的标头是:
我在 CreateAuthorizationHeader(String canonicalizedString)中有断点,然后复制 utcDate 和 Signature 的值并将其传递给标题。我要去哪里错了??
答案 0 :(得分:0)
出现此错误的原因是因为您将content-type
作为请求标头之一传递,但是在计算canonicalizedString
时却没有包括它。
您可以做两件事:
content-type
:由于您正在获取Blob的内容,因此您实际上不需要此标头。删除该代码后,代码应该可以正常工作。content-type
计算中包含image/jpeg
标头(canonicalizedString
)的值:因此您的代码应为: string authStr = "GET\n\n\n\n\n\n\n\n\n\n\n\nimage/jpeg\nx-ms-date:" + utcDate + "\nx-ms-version:2018-03-28\n/<account_name>/<container_name>/<image.jpeg>";