【发布时间】:2019-02-06 11:55:46
【问题描述】:
我正在尝试使用邮递员通过共享密钥读取存储在 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>";
}
我传递的标题是:
- 内容类型:图片/jpeg
- 授权:SharedKey account_name:jn37EV4KPWj3wQANreUQy8ih+H5rFOp0fqj1DebgBMk=
- x-ms-版本:2018-03-28
- x-ms-date:格林威治标准时间 2019 年 2 月 6 日星期三 10:38:54
我在 CreateAuthorizationHeader(String canonicalizedString) 中有断点,我复制了 utcDate 和 Signature 的值并将其传递给标题。我哪里错了?
【问题讨论】:
标签: c# azure azure-storage