所以我只是試圖列出存儲帳戶中的表以使用Query Tables方法來測試授權。我嘗試使用SDK,但SDK試圖引用RT中不可用的DLL。決定試用REST API。但我有這個天賦http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspxWindows Azure存儲的授權標題Windows Store應用程序的REST API
public async Task ExecuteAsync()
{
try
{
HttpClient client = new HttpClient();
Dictionary<string, string> headers = GetHeaders("/Tables");
client.DefaultRequestHeaders.Date = DateTimeOffset.Parse(headers["Date"]);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("SharedKey", headers["Authorization"]);
const string url = "http://account-name.table.core.windows.net/Tables";
XmlReader reader = XmlReader.Create(await client.GetStreamAsync(url));
//
// Do some stuff with the reader here
//
}
catch (Exception e)
{
// handle exception
}
}
public Dictionary<string, string> GetHeaders(string resource)
{
Dictionary<string, string> headers = new Dictionary<string, string>();
headers["Date"] = DateTime.Now.ToString("R");
headers["Authorization"] = GetAuthorizationHeader(resource, headers["Date"]);
return headers;
}
public string GetAuthorizationHeader(string resource, string date)
{
const string key = PRIMARY_KEY;
const string accountName = ACCOUNT_NAME;
string signee = string.Join("\n", new List<string> { "GET", "", "", date, resource });
// make the signature
MacAlgorithmProvider hmac = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA256");
IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
CryptographicKey hmacKey = hmac.CreateKey(keyMaterial);
IBuffer data = CryptographicBuffer.ConvertStringToBinary(signee, BinaryStringEncoding.Utf8);
IBuffer hash = CryptographicEngine.Sign(hmacKey, data);
string signature = CryptographicBuffer.EncodeToBase64String(hash);
return string.Format("{0}:{1}", accountName, signature);
}
與認證的麻煩顯然我失去了一些東西,因爲我不斷地得到403的。通過此代碼查看任何問題?
應該提到我嘗試了不同的CanonizedResources而不僅僅是/ Tables,我還嘗試了/ accountname/Tables和其他一些 – agentargo 2013-04-05 21:31:29