所以我只是尝试列出存储帐户中的表,以使用Query Tables方法测试授权。我尝试使用SDK,但SDK试图引用RT中不可用的DLL。决定试用REST API。但我在使用此规范http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
进行身份验证时遇到问题 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。查看此代码时遇到的任何问题?
答案 0 :(得分:1)
一些评论:
还有一个用于Windows RT的存储客户端库。请在这里查看我的答案:Working with Azure in Winrt with Rest API, trouble with signature。
出现问题,您可以尝试更改以下代码行:
headers["Date"] = DateTime.Now.ToString("R");
到
headers["Date"] = DateTime.UtcNow.ToString("R");
看看是否有帮助。
<强>更新强>
我还注意到您正在使用CryptographicBuffer.ConvertStringToBinary
将Base64编码的密钥转换为字节。请尝试使用CryptographicBuffer.DecodeFromBase64String
(http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.cryptographicbuffer.decodefrombase64string.aspx)代替。