我在Postman中发送以下请求,以在此URL https://steamo.blob.core.windows.net/testcontainer/dog.jpg从Azure Blob存储中检索一个简单的.jpg
GET /testcontainer/dog.jpg HTTP/1.1
Host: steamo.blob.core.windows.net
Authorization: SharedKey steamo:<my access key>
x-ms-date: Tue, 26 May 2015 17:35:00 GMT
x-ms-version: 2014-02-14
Cache-Control: no-cache
Postman-Token: b1134f8a-1a03-152c-2810-9cb351efb9ce
如果你不熟悉Postman,它只是一个REST客户端——post - token头可以忽略。
我的访问密钥是从Azure管理门户复制的。
我得到这个错误:
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:2482503d-0001-0033-60da-9708ed000000 Time:2015-05-26T17:35:41.4577821Z
使用此authenticationerrordetail:
The MAC signature found in the HTTP request '<my access key>' is not the same as any computed signature. Server used following string to sign: 'GET x-ms-date:Tue, 26 May 2015 17:35:00 GMT x-ms-version:2014-02-14 /steamo/testcontainer/dog.jpg'.
如何解决这个问题?
Azure存储的身份验证不仅仅是提供访问密钥的问题(这不是很安全)。您需要创建一个表示给定请求的签名字符串,使用HMAC-SHA256算法对字符串进行签名(使用您的存储密钥进行签名),并以64进制对结果进行编码。详细信息请参见https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx,包括如何构造签名字符串。
刚刚完成,下面是我的代码:
string signWithAccountKey(string stringToSign, string accountKey)
{
var hmacsha = new System.Security.Cryptography.HMACSHA256();
hmacsha.Key = Convert.FromBase64String(accountKey);
var signature = hmacsha.ComputeHash(Encoding.UTF8.GetBytes(stringToSign));
return Convert.ToBase64String(signature);
}