【问题标题】:Accessing a blob resource using shared key authorization使用共享密钥授权访问 blob 资源
【发布时间】: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>";
    }

我传递的标题是:

  1. 内容类型:图片/jpeg
  2. 授权:SharedKey account_name:jn37EV4KPWj3wQANreUQy8ih+H5rFOp0fqj1DebgBMk=
  3. x-ms-版本:2018-03-28
  4. x-ms-date:格林威治标准时间 2019 年 2 月 6 日星期三 10:38:54

我在 CreateAuthorizationHeader(String canonicalizedString) 中有断点,我复制了 utcDateSignature 的值并将其传递给标题。我哪里错了?

【问题讨论】:

    标签: c# azure azure-storage


    【解决方案1】:

    您收到此错误的原因是因为您将 content-type 作为请求标头之一传递,但是在计算 canonicalizedString 时未包含它。

    你可以做两件事:

    1. 从您的请求标头中删除 content-type:由于您正在获取 blob 的内容,因此您实际上并不需要此标头。删除它后,代码应该可以正常工作。
    2. 在您的 canonicalizedString 计算中包含 content-type 标头 (image/jpeg) 的值:因此您的代码将是:

    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/&lt;account_name&gt;/&lt;container_name&gt;/&lt;image.jpeg&gt;";

    【讨论】:

    • 我在标头中使用了 Content-Type,并在 canonicalizedString 中包含了 image/jpeg(content-type) 并且它起作用了。谢谢您的帮助。 :)
    猜你喜欢
    • 2019-07-01
    • 1970-01-01
    • 2013-06-19
    • 2018-10-11
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多