我试图在我的windows azure存储帐户中列出容器。但是我遇到了异常和#34;远程服务器返回错误:(403)服务器无法验证请求。确保正确形成Authorization标头的值,包括签名.."
但是我按照给出的说明包含了签名,是否有人在我的代码中发现任何错误?
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();
}
}
}