2017-02-25 72 views
1

我需要幫助構建授權標頭來放置塊blob。在Postman中使用Blob服務API傳遞blob - 授權標頭

PUT \ n \ n \ n11 \ n \ n \ n \ n \ n \ n \ n \ n \ n \ nx-ms-blob-type:BlockBlob \ nx-ms-date:2017年2月25日:20:13 GMT \ nx-ms-version:2015-02-21 \ n/myaccountname/mycontainername/blob.txt \ n

我把這個,UTF 8編碼它。然後,我在我的Azure帳戶中使用我的訪問密鑰,並將HMAC sha256 UTF 8編碼的字符串與密鑰關聯起來。然後我用base64輸出。我們來調用這個輸出字符串。

我的授權頭看起來是這樣的:SharedKey myaccountname:輸出字符串

這是行不通的。

郵差中的郵件頭也有x-ms-blob-type,x-ms-date,x-ms-version,Content-Length和Authorization。現在的身體說,你好世界。

任何人都可以幫助我在郵遞員這個成功的要求嗎?

<?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:cdeb9a5e-0001-0029-5fb5-8f7995000000 
Time:2017-02-25T22:22:32.0300016Z</Message> 
    <AuthenticationErrorDetail>The MAC signature found in the HTTP request 'jiJtirohvi1syXulqkPKESnmQEJI4GpDU5JBn7BM/xY=' is not the same as any computed signature. Server used following string to sign: 'PUT 


11 

text/plain;charset=UTF-8 






x-ms-date:Sat, 25 Feb 2017 22:20:13 GMT 
x-ms-version:2015-02-21 
/myaccountname/mycontainername/blob.txt'.</AuthenticationErrorDetail> 
</Error> 

編輯:

首先,我要感謝你,大家誰迴應。我真的很感激它。我有最後一個問題,然後我想我會被設置!我沒有使用該代碼 - 我手動完成這一切。如果我有我的密鑰:X2iiy6v47j1jZZH5555555555zzQRrIAdxxVs55555555555av8uBungcBMotmS7tDqas14gU5O/w ==稍微改變了一下匿名 - 我解碼它:使用在線base64解碼器。然後,當我有我的字符串,現在看起來像這樣:PUT \ n \ n \ n11 \ n \ ntext \ plain; charset = UTF-8 \ n \ n \ n \ n \ n \ n \ n \ nx-ms-blob -type:BlockBlob \ nx-ms-date:Mon,27 Feb 2017 21:53:13 GMT \ nx-ms-version:2015-02-21 \ n/myaccount/mycontainer/blob.txt \ n所以我運行這個在https://mothereff.in/utf-8然後在HMAC中用我的解碼密鑰:https://www.liavaag.org/English/SHA-Generator/HMAC/ - 在末尾使用sha256和base64。那是我如何得到正確的字符串放到這裏:SharedKey我的帳戶:

+0

由於「\ n」字符,使用您提到生成的代碼與簽名不匹配的工具。我寫了一個[在線工具](https://onlineencrypt.herokuapp.com/)來生成簽名。我也更新了答案。 –

回答

0

我相信有一個與你怎麼會在這裏指定StringToSign一個問題:

PUT \ n \ n \ N11 \ n \ n \ nx-ms-blob-type:BlockBlob \ nx-ms-date:星期六, 2017年2月25日22時20分13秒 GMT \ nx-ms-version:2015 -02-21 \ N/myaccountname/mycontainername/blob.txt \ n

如果您發現從服務器返回的錯誤信息,串,服務器簽名比你的不同,不同的是服務器在簽名計算中使用的是Content-Typetext/plain;charset=UTF-8),而不是。請在您的代碼中包含此內容類型,並且應該可以正常工作。

這裏的示例代碼(僅部分)我用:

 var requestMethod = "PUT"; 
     var urlPath = "test" + "/" + "myblob.txt"; 
     var storageServiceVersion = "2015-12-11"; 
     var date = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture); 
     var blobType = "BlockBlob"; 
     var contentBytes = Encoding.UTF8.GetBytes("Hello World"); 
     var canonicalizedResource = "/" + accountName + "/" + urlPath; 
     var canonicalizedResource = "/" + accountName + "/" + urlPath; 
     var canonicalizedHeaders = "x-ms-blob-type:" + blobType + "\nx-ms-date:" + date + "\nx-ms-version:" + storageServiceVersion + "\n"; 
     var stringToSign = requestMethod + "\n" + 
      "\n" + //Content Encoding 
      "\n" + //Content Language 
      "11\n" + //Content Length 
      "\n" + //Content MD5 
      "text/plain;charset=UTF-8" + "\n" + //Content Type 
      "\n" + //Date 
      "\n" + //If - Modified - Since 
      "\n" + //If - Match 
      "\n" + //If - None - Match 
      "\n" + //If - Unmodified - Since 
      "\n" + //Range + 
      canonicalizedHeaders + 
      canonicalizedResource; 
     string authorizationHeader = GenerateSharedKey(stringToSign, accountKey, accountName); 


    private static string GenerateSharedKey(string stringToSign, string key, string account) 
    { 
     string signature; 
     var unicodeKey = Convert.FromBase64String(key); 
     using (var hmacSha256 = new HMACSHA256(unicodeKey)) 
     { 
      var dataToHmac = Encoding.UTF8.GetBytes(stringToSign); 
      signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac)); 
     } 
     return string.Format(CultureInfo.InvariantCulture, "{0} {1}:{2}", "SharedKey", account, signature); 
    } 
0

根據您的錯誤信息,則表明授權簽名不正確。

如果Content-Type "text/plain; charset=UTF-8"未包含在標題中,請將其添加到stringTosign和postman中。

當我們嘗試獲取簽名時,我們需要確保length of the blob.txt與stringTosign中的Content length匹配。這意味着請求主體長度應該與stringTosign中的內容長度相匹配。

我用郵差測試它,它工作正常。我們可以在另一個SO Thread中獲得代碼簽名。以下是我的詳細步驟

  1. 添加下列頭

enter image description here

  • 添加請求體(例如:的Hello World)
  • enter image description here

    1. 發送put blob請求。

    enter image description here

    更新:

    請試試看使用online tool爲測試生成簽名。