2011-01-06 82 views
1

我正在使用此代碼直接使用rest api將一些文本上傳到azure blob。我正在禁止一個webexception 403。誰能plaese告訴我,我在我的代碼獲取403 azure blob放置請求禁止webexception

private String CreateAuthorizationHeader(String canonicalizedString, CloudBlob blob) 
    { 
     String signature = string.Empty; 


     using (HMACSHA256 hmacSha256 = new HMACSHA256()) 
     { 
      Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(canonicalizedString); 
      signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac)); 
     } 


     String authorizationHeader = String.Format(CultureInfo.InvariantCulture, "{0} {1}:{2}", "SharedKeyLite", myaccount, signature); 

     return authorizationHeader; 
    } 

    private void PutBlob(String containerName, String blobName , CloudBlob blob) 
    { 
     String requestMethod = "PUT"; 

     String urlPath = String.Format("{0}", blobName); 

     String storageServiceVersion = "2009-10-01"; 

     String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture); 

     // if (uploadPST.HasFile) 
     // { 
      string content = "Sample file"; 
      // Stream content = uploadPST.FileBytes; 
      UTF8Encoding utf8Encoding = new UTF8Encoding(); 
      Byte[] blobContent = utf8Encoding.GetBytes(content); 
      Int32 blobLength = blobContent.Length; 


      const String blobType = "BlockBlob"; 

      /* String canonicalizedHeaders = String.Format(
        "x-ms-blob-type:{0}\nx-ms-date:{1}\nx-ms-version:{2}", 
        blobType, 
        dateInRfc1123Format, 
        storageServiceVersion);*/ 

      String canonicalizedHeaders = String.Format(
        "x-ms-date:{0}\nx-ms-meta-m1:{1}\nx-ms-meta-m1:{2}", 
        dateInRfc1123Format, 
        "v1", 
        "v2"); 

      String canonicalizedResource = String.Format("/{0}/{1}", myaccount, urlPath); 

      String stringToSign = String.Format(
        "{0}\n\n{1}\n\n{2}\n{3}", 
        requestMethod, 
        "text/plain; charset=UTF-8", 
        canonicalizedHeaders, 
        canonicalizedResource); 



      String authorizationHeader = CreateAuthorizationHeader(stringToSign, blob); 

      Uri uri = new Uri(CloudStorageAccount.FromConfigurationSetting("DataConnectionString").BlobEndpoint + "/" + urlPath); 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
      request.Method = requestMethod; 


      // request.Headers.Add("x-ms-blob-type", blobType); 
      request.Headers.Add("x-ms-date", dateInRfc1123Format); 
      // request.Headers.Add("x-ms-version", storageServiceVersion); 
      request.Headers.Add("Authorization", authorizationHeader); 
      request.ContentLength = blobLength; 

      using (Stream requestStream = request.GetRequestStream()) 
      { 
       requestStream.Write(blobContent ,0 ,blobLength); 
      } 

      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
      { 
       String ETag = response.Headers["ETag"]; 
      } 
     // } 
    } 
+0

僅供參考他人,這是正在上討論MSDN上的Windows Azure論壇:http://social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/0526ccc4-63e3-404f-a355-28179b6e88cb – smarx 2011-01-06 09:37:12

回答

0

首先去錯了,你構建使用默認的構造函數HMACSHA256對象 - 這將導致生成並用於簽名隨機密鑰。你想要的是接受一個字符串的超載 - 並傳遞天藍色的帳戶密鑰。

儘管如此,手動簽名請求可能會非常棘手,因爲有很多事情要做,而且很容易搞砸或忘記一些東西。相反,我建議您使用StorageCredentialsAccountAndKey類的SignRequest方法(msdn doc)例如;

// ...there exists a request object, and strings for the account name and key

var creds = new StorageCredentialsAccountAndKey(accountName, accountKey);
creds.SignRequest(request);

這將做正確簽名的請求,包括創建一個規範化的標題字符串所需要的一切,創建具有正確格式的日期等