本文介绍了Windows Azure的REST API列出容器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出我的Windows Azure存储帐户的容器。但我触动了异常结果远程服务器返回错误:(403)服务器无法验证请求确认授权头的值是正确形成,包括签名...。

I'm trying to list the containers in my windows azure storage account. but i'm struck with an exception
"The remote server returned an error: (403) Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.."

但我还包括签名按所给的指示,做任何一个找到我的code任何错误呢?

But i have included the signature as per the instructions given, do any one find any mistake in my code ?

private static String SignThis(string StringToSign,string  Key,string  Account)
        {

            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(
                System.Globalization.CultureInfo.InvariantCulture,
                "{0} {1}:{2}",
                "SharedKey",
                Account,
                signature);
            return authorizationHeader;
        }
        static void ListContainers()
        {
            Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
            string Key = @"MyStorageAccountKey";
            string Account = @"MyStorageAccountName";

            DateTime dt = DateTime.UtcNow;

            string dataStr = dt.ToString ("R",System.Globalization.CultureInfo.InvariantCulture);
            string StringToSign = String.Format("GET\n"
                + "\n" // content encoding
                + "\n" // content language
                + "\n" // content length
                + "\n" // content md5
                + "\n" // content type
                + "\n" // date
                + "\n" // if modified since
                + "\n" // if match
                + "\n" // if none match
                + "\n" // if unmodified since
                + "\n" // range
                + "x-ms-date:" + dataStr + "\nx-ms-version:2014-02-14\n" // headers
                + "/{0}\ncomp:list", Account);

            string auth = SignThis(StringToSign, Key, Account);
            string method = "GET";
            string urlPath = string.Format ("https://{0}.blob.core.windows.net/?comp=list", Account);
            Uri uri = new Uri(urlPath);
            HttpWebRequest reque = (HttpWebRequest)WebRequest.Create(uri);
            reque.Method = method;
            reque.Headers.Add("Authorization", auth);
            reque.Headers.Add("x-ms-date",dataStr);
            reque.Headers.Add("x-ms-version", "2014-02-14");

            using (HttpWebResponse response = (HttpWebResponse) reque.GetResponse ()) {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string text = reader.ReadToEnd();
                }
            }
        }

编辑:字符串我用来生成签名

Edit : String i Used to generate Signature

GET

x-ms-date:Tue, 14 Jul 2015 18:38:16 GMT
x-ms-version:2014-02-14
/MyStorageAccountName/
comp:list

编辑:我收到的异常响应:

Edit : I received the exception response :

<?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:2fc74ef8-0001-0083-2664-be8850000000
Time:2015-07-14T18:38:18.0831721Z</Message><AuthenticationErrorDetail>The MAC signature found in the HTTP request '5rqWNl2i8kuZF6haCRqFr1S0viOM9eLjz4L/zU6GCsg=' is not the same as any computed signature. Server used following string to sign: 'GET

x-ms-date:Tue, 14 Jul 2015 18:38:16 GMT
x-ms-version:2014-02-14
/MyStorageAccountName/
comp:list'.</AuthenticationErrorDetail></Error>

最后编辑:使由gauvrav指定的所有更改后,我发现我使用的storagekey错了,更换正确的之后,它工作正常。

Final After making all the changes specified by gauvrav, i found that the storagekey i used was wrong, after replacing the right one, it is working fine.

有可能是此错误的其他变化:请参照本link

There may be other changes for this error: please refer this link

推荐答案

请改变你的 StringToSign 来:

        string StringToSign = String.Format("GET\n"
            + "\n" // content encoding
            + "\n" // content language
            + "\n" // content length
            + "\n" // content md5
            + "\n" // content type
            + "\n" // date
            + "\n" // if modified since
            + "\n" // if match
            + "\n" // if none match
            + "\n" // if unmodified since
            + "\n" // range
            + "x-ms-date:" + dataStr + "\nx-ms-version:2014-02-14\n" // headers
            + "/{0}/\ncomp:list", Account);//Notice an extra "/" after "{0}"

据帐户名称占位符后缺少 / (在code最后一行以上)。一旦你这样做,你应该能够看到XML格式返回容器列表。

It was missing a / after account name placeholder (last line in the code above). Once you do that, you should be able to see the list of containers returned in XML format.

这篇关于Windows Azure的REST API列出容器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 22:43