访问使用共享密钥授权一个blob资源

问题描述 投票:0回答:1

我想读通过共享密钥使用邮递员这是给我下面的错误存储在微软的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年3月28日
  4. X-MS-日期:星期三,2019年2月6日10点38分54秒GMT

我在CreateAuthorizationHeader(字符串canonicalizedString)断点和我复制utcDate和签名的值,并把它传递给头。我要去哪里错了?

c# azure azure-storage
1个回答
0
投票

你得到这个错误的原因是因为你传递content-type的请求头但你不包括它,当你计算canonicalizedString之一。

这里有两种事情可以做:

  1. 从您的请求头中取出content-type:既然你得到的blob的内容,你并不真的需要这个头。一旦你删除,代码应该只是罚款。
  2. 包括在你的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>";

© www.soinside.com 2019 - 2024. All rights reserved.