admin管理员组文章数量:1335664
I've been stuck for the whole day making my first call of Azure Storage REST API. Postman's response showed that it's due to error in Azure authentication, but I have no idea what's the problem.
Here is the browser script to send Azure Storage REST API:
function azureListContainers() {
var key = "key-copied-from-azure-storage-account";
var strTime = (new Date()).toUTCString();
var strToSign = 'GET\n\n\n\nx-ms-date:' + strTime + '\nx-ms-version:2015-12-11\n/myaccount/?p=list';
var hash = CryptoJS.HmacSHA256(strToSign, key);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var auth = "SharedKeyLite myaccount:"+hashInBase64;
console.log(strToSign);
console.log(auth);
console.log(strTime);
$.ajax({
type: "GET",
beforeSend: function (request)
{
request.setRequestHeader("Authorization", auth);
request.setRequestHeader("x-ms-date", strTime);
request.setRequestHeader("x-ms-version", "2015-12-11");
},
url: "/?p=list",
processData: false,
success: function(msg) {
console.log(msg);
}
});
}
Chrome Developer Tool just returned No 'Access-Control-Allow-Origin' header without further reason, so I copied the contents of var auth
and var strTime
, created the same request using Postman tool:
[Command]
GET /?p=list
[Headers]
Authorization:SharedKeyLite myaccount:Z9/kY/D+osJHHz3is+8yJRqhj09VUlr5n+PlePUa8Lk=
x-ms-date:Tue, 09 Aug 2016 10:30:49 GMT
x-ms-version:2015-12-11
[Response Body]
<?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:9be3d595-0001-0012-4929-f2fde2000000
Time:2016-08-09T10:31:52.6542965Z</Message>
<AuthenticationErrorDetail>The MAC signature found in the HTTP request 'Z9/kY/D+osJHHz3is+8yJRqhj09VUlr5n+PlePUa8Lk=' is not the same as any puted signature. Server used following string to sign: 'GET
x-ms-date:Tue, 09 Aug 2016 10:30:49 GMT
x-ms-version:2015-12-11
/myaccount/?p=list'.</AuthenticationErrorDetail>
</Error>
After diff the two strings, I believe var strToSign
in my script is the same as the string Azure used to sign. But still there was an authentication error. Please help indicate what's the problem.
I've been stuck for the whole day making my first call of Azure Storage REST API. Postman's response showed that it's due to error in Azure authentication, but I have no idea what's the problem.
Here is the browser script to send Azure Storage REST API:
function azureListContainers() {
var key = "key-copied-from-azure-storage-account";
var strTime = (new Date()).toUTCString();
var strToSign = 'GET\n\n\n\nx-ms-date:' + strTime + '\nx-ms-version:2015-12-11\n/myaccount/?p=list';
var hash = CryptoJS.HmacSHA256(strToSign, key);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var auth = "SharedKeyLite myaccount:"+hashInBase64;
console.log(strToSign);
console.log(auth);
console.log(strTime);
$.ajax({
type: "GET",
beforeSend: function (request)
{
request.setRequestHeader("Authorization", auth);
request.setRequestHeader("x-ms-date", strTime);
request.setRequestHeader("x-ms-version", "2015-12-11");
},
url: "https://myaccount.blob.core.windows/?p=list",
processData: false,
success: function(msg) {
console.log(msg);
}
});
}
Chrome Developer Tool just returned No 'Access-Control-Allow-Origin' header without further reason, so I copied the contents of var auth
and var strTime
, created the same request using Postman tool:
[Command]
GET https://myaccount.blob.core.windows/?p=list
[Headers]
Authorization:SharedKeyLite myaccount:Z9/kY/D+osJHHz3is+8yJRqhj09VUlr5n+PlePUa8Lk=
x-ms-date:Tue, 09 Aug 2016 10:30:49 GMT
x-ms-version:2015-12-11
[Response Body]
<?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:9be3d595-0001-0012-4929-f2fde2000000
Time:2016-08-09T10:31:52.6542965Z</Message>
<AuthenticationErrorDetail>The MAC signature found in the HTTP request 'Z9/kY/D+osJHHz3is+8yJRqhj09VUlr5n+PlePUa8Lk=' is not the same as any puted signature. Server used following string to sign: 'GET
x-ms-date:Tue, 09 Aug 2016 10:30:49 GMT
x-ms-version:2015-12-11
/myaccount/?p=list'.</AuthenticationErrorDetail>
</Error>
After diff the two strings, I believe var strToSign
in my script is the same as the string Azure used to sign. But still there was an authentication error. Please help indicate what's the problem.
1 Answer
Reset to default 7Chrome Developer Tool just returned No 'Access-Control-Allow-Origin' header
As you are using javascript to send http requests against to Azure Storage Server directly from client. It's a mon CORS issue.
When you occur this issue, you can enable the CORS for your storage services. For simple, you can leverage Microsoft Azure Storage Explorer to configure your storage.
AuthenticationFailed
I did two modifications based on your code snippet, in my test project to make it work.
var hash = CryptoJS.HmacSHA256(strToSign, key);
The second parameter, should be a base64 decode from the account key, refer to the Azure Storage SDK for node.js- In my test, I had to use
SharedKey
scheme and authenticate withSharedKey
token to make your scenario to work.
Here is my test code snippet, FYI.,
var key = "key-copied-from-azure-storage-account";
var strTime = (new Date()).toUTCString();
var strToSign = 'GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:' + strTime + '\nx-ms-version:2015-12-11\n/<accountname>/\np:list';
var secret = CryptoJS.enc.Base64.parse(key);
var hash = CryptoJS.HmacSHA256(strToSign, secret);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var auth = "SharedKey <accountname>:"+hashInBase64;
Additionally, for security reason, please implement the Azure Storage Services in backend web server. As using pure javascript on client, will expose your account name and account key to public, which will raise the risk to your data and info.
本文标签: javascriptAuthorization of Azure Storage service REST APIStack Overflow
版权声明:本文标题:javascript - Authorization of Azure Storage service REST API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742270282a2444176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论