【问题标题】:The MAC signature found in the HTTP request is not the same as any computed signature在 HTTP 请求中找到的 MAC 签名与任何计算的签名不同
【发布时间】:2015-06-08 06:50:08
【问题描述】:

我已经搜索了几天了,这个 azure auth 正在杀死我。我不断收到标题中的错误。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace netTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var b = new bt();
            b.GetBlob_Test();
            Console.ReadLine();
        }

        public class bt
        {

            string accessKey = "4LVWlzPCPA5k45VDAPJJU6zXK6KvycT142Z8owbQv1m8bPm+cZPQamDKne/Uq4BHjAb9QR8bpanvoIgyOydcOg==";
            string accountName = "trikegirlstudio";
            string container = "sgm"; 

            // GetBlob_Test
            public void GetBlob_Test()
            {
                Console.WriteLine("Attempting to GET from server");
                DateTime dt = DateTime.UtcNow;

                string stringToSign = String.Format("GET\n"
                                                    + "\n" // content md5
                                                    + "\n" // content type
                                                    + "x-ms-date:" + dt.ToString("R") + "x-ms-version:2009-09-19\n" + "\n" // headers
                                                    + "/{0}/{1}\ncomp:list\nrestype:container", accountName, container);

                string authorizationKey = SignThis(stringToSign, accessKey, accountName);
                string method = "GET";
                string urlPath = string.Format("http://{0}.blob.core.windows.net/{1}?restype=container&comp=list", accountName, container);
                Uri uriTest = new Uri(urlPath);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriTest);
                request.Proxy = new WebProxy("127.0.0.1", 8888);
                request.Method = method;
                request.Headers.Add("x-ms-date", dt.ToString("R"));
                request.Headers.Add("x-ms-version", "2009-09-19");
                request.Headers.Add("Authorization", authorizationKey);

                Console.WriteLine("Authorization: " + authorizationKey);

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Console.WriteLine("Response = " + response);
                }
            }
            private static String SignThis(String StringToSign, string Key, string Account)
            {
                //StringToSign = 
                String signature = string.Empty;
                byte[] unicodeKey = Convert.FromBase64String(Key);
                using (HMACSHA256 hmacSha256 = new HMACSHA256(unicodeKey))
                {
                    Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(StringToSign);
                    signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
                }

                String authorizationHeader = String.Format(
                    CultureInfo.InvariantCulture,
                    "{0} {1}:{2}",
                    "SharedKeyLite",
                    Account,
                    signature);
                Console.WriteLine(StringToSign);
                return authorizationHeader;
            }
        }
    }
}

任何帮助将不胜感激 我真的看不出哪里出了问题

编辑:: 完全错误

AuthenticationFailedServer 未能验证请求。确保 Authorization 标头的值正确形成,包括签名。 请求ID:a8098960-0001-0003-3d38-a2fc0d000000 时间:2015-06-08T22:14:39.9876002Z 在 HTTP 请求“kmOqr60n0Nus1HJ1xoaplISdTEk8Hfdj9BIJK74Ojow=”中找到的 MAC 签名与任何计算的签名都不相同。服务器使用以下字符串进行签名:'GET

x-ms-date:格林威治标准时间 2015 年 6 月 8 日星期一 22:14:41 x-ms-版本:2009-09-19 /trikegirlstudio/sgm?comp=list'。

【问题讨论】:

  • 请添加错误信息

标签: c# azure


【解决方案1】:

查看此处的文档:https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx(第 Constructing the Canonicalized Headers String 部分,第 6 点)

最后,在每个规范化的标头中附加一个换行符 结果列表。通过以下方式构造 CanonicalizedHeaders 字符串 将此列表中的所有标题连接成一个字符串。

我认为这段代码导致了错误:

string stringToSign = String.Format("GET\n"
                                                    + "\n" // content md5
                                                    + "\n" // content type
                                                    + "x-ms-date:" + dt.ToString("R") + "x-ms-version:2009-09-19\n" + "\n" // headers
                                                    + "/{0}/{1}\ncomp:list\nrestype:container", accountName, container);

如果您在代码中注意到,您的 x-ms-datex-ms-version 标头之间没有 new line character。在x-ms-version之后还有一个额外的换行符。

你的代码应该是:

string stringToSign = String.Format("GET\n"
                                                    + "\n" // content md5
                                                    + "\n" // content type
                                                    + "x-ms-date:" + dt.ToString("R") + "\nx-ms-version:2009-09-19\n" // headers
                                                    + "/{0}/{1}\ncomp:list\nrestype:container", accountName, container);

试试看。它应该可以解决问题。

【讨论】:

猜你喜欢
  • 2015-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 2022-11-02
  • 2022-01-18
相关资源
最近更新 更多