HTTP请求中找到的MAC签名与任何计算签名不同

时间:2015-06-08 06:50:09

标签: c# azure

我现在已经搜遍了几天,而这位天蓝色的作家正在杀了我。我不断从标题中得到错误。

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;
            }
        }
    }
}

非常感谢任何帮助 我真的看不出哪里出错了

编辑::完整错误

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

x-ms-date:Mon,08 Jun 2015 22:14:41 GMT X-MS-版本:2009-09-19 / trikegirlstudio / SGM?排版=列表”。

1 个答案:

答案 0 :(得分:0)

查看此处的文档:https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx(第if segue.identifier == "bookingPopOverSegue" { var bookingViewController = segue.destinationViewController as! BookingViewController var passthroughViews: [AnyObject] = self.timeSlotButtons passthroughViews.append(self.scrollView) bookingViewController.popoverPresentationController?.passthroughViews = passthroughViews } 部分,第6点)

  

最后,在每个规范化的标题中添加一个换行符   结果列表。构造CanonicalizedHeaders字符串   将此列表中的所有标题连接成一个字符串。

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

Constructing the Canonicalized Headers String

如果您在代码中注意到,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); new line character标题之间没有x-ms-date。在x-ms-version之后还有一个额外的换行符。

您的代码应为:

x-ms-version

试一试。它应该解决问题。