【问题标题】:Authorization of Azure Storage service REST APIAzure 存储服务 REST API 的授权
【发布时间】:2016-12-15 10:04:26
【问题描述】:

我在第一次调用 Azure 存储 REST API 时被困了一整天。 Postman 的回复显示是 Azure 身份验证出错,但我不知道是什么问题。

这是发送 Azure 存储 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/?comp=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.net/?comp=list",
    processData: false,
    success: function(msg) {
        console.log(msg);
    }
});
}

Chrome 开发者工具无故返回 No 'Access-Control-Allow-Origin' 标头,所以我复制了 var authvar strTime 的内容,使用 Postman 工具创建了相同的请求:

[Command]
GET https://myaccount.blob.core.windows.net/?comp=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 computed 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/?comp=list'.</AuthenticationErrorDetail>
</Error>

区分这两个字符串后,我相信我的脚本中的var strToSign 与Azure 用于签名的字符串相同。但仍然存在身份验证错误。请帮忙指出问题所在。

【问题讨论】:

    标签: javascript azure-storage


    【解决方案1】:

    Chrome 开发者工具刚刚返回 No 'Access-Control-Allow-Origin' 标头

    当您使用 javascript 直接从客户端向 Azure 存储服务器发送 http 请求时。这是一个常见的CORS 问题。

    当您出现此问题时,您可以enable the CORS for your storage services。为简单起见,您可以利用Microsoft Azure Storage Explorer 来配置您的存储。

    身份验证失败

    在我的测试项目中,我根据你的代码 sn-p 做了两处修改以使其工作。

    1. var hash = CryptoJS.HmacSHA256(strToSign, key);第二个参数,应该是一个base64解码的account key,参考Azure Storage SDK for node.js
    2. 在我的测试中,我必须使用SharedKey 方案并使用SharedKey 令牌进行身份验证才能使您的方案正常工作。

    这是我的测试代码 sn-p,仅供参考,

    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>/\ncomp: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; 
    

    另外,出于安全考虑,请在后端 Web 服务器中实现 Azure 存储服务。由于在客户端使用纯 javascript,会将您的帐户名称和帐户密钥公开,这将增加您的数据和信息的风险。

    【讨论】:

    • 谢谢你救了我的命,加里!作为一个菜鸟,直接来自this Authentication doc,我不知道有必要先对密钥进行base64解码。如果可能,请在文档中添加示例代码 sn-p 以供其他人快速入门。 :-)
    • 很高兴听到这个消息。
    • 这似乎适用于 blob,但不适用于 Table。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2017-10-23
    • 1970-01-01
    • 2021-05-31
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多