2013-03-24 64 views
1

我试图在winrt中使用azure存储。由于azure存储客户端与winrt不兼容,我正在尝试使用azure的rest API。我有一段时间得到签名的权利,我可以用另一双眼睛来帮助我看到我要出错的地方。在Restrt中使用Azure与Rest API,签名时遇到问题

Azure帐户提供了一个名称和关键属性,此方法立即建立请求,只需列出所有的blob。

'private async void BuildHTTPRequest(AzureAccount account) { System.Net.Http.HttpClient request = new HttpClient();

request.BaseAddress = new Uri(string.Format("http://{0}.blob.core.windows.net/", account.Name)); 

    // Always have to use UTC date/time 
    request.DefaultRequestHeaders.Add("x-ms-date", DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture)); 

    string fmtStringToSign = "{0}\n{1}\n{2}\n{3:R}\n{4}{5}"; 

    request.DefaultRequestHeaders.Add("x-ms-version", "2011-08-18"); 

    string hdr = CanonicalizeHeaders(request.DefaultRequestHeaders); 
    string authValue = string.Format(fmtStringToSign, "GET", "", "", "", hdr, ""); 
    byte[] signatureByteForm = System.Text.Encoding.UTF8.GetBytes(authValue); 



    string hashKey = account.Key; 

    MacAlgorithmProvider macAlgorithmProvider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA256"); 
    BinaryStringEncoding encoding = BinaryStringEncoding.Utf8; 
    var messageBuffer = CryptographicBuffer.ConvertStringToBinary(authValue, encoding); 
    IBuffer keyBuffer = CryptographicBuffer.ConvertStringToBinary(hashKey, encoding); 
    CryptographicKey hmacKey = macAlgorithmProvider.CreateKey(keyBuffer); 
    IBuffer signedMessage = CryptographicEngine.Sign(hmacKey, messageBuffer); 

    string hashedString = CryptographicBuffer.EncodeToBase64String(signedMessage); 

    String authHeader = String.Format(CultureInfo.InvariantCulture, "{0} {1}:{2}", "SharedKey", 
    account.Name, hashedString); 

    request.DefaultRequestHeaders.Add("Authorization", authHeader); 

    // Send the request to the queue 
    try 
    { 
     var test1 = request.GetAsync("?comp=list").Result; 
     if (test1.IsSuccessStatusCode) 
     { 

     } 
    } 
    catch (WebException ex) { } 


} 

这应该设置页眉弥补签约...

public string CanonicalizeHeaders(System.Net.Http.Headers.HttpRequestHeaders hdrCollection) 
{ 
    StringBuilder retVal = new StringBuilder();// Look for header names that start with "x-ms-" // Then sort them in case-insensitive manner. 

    List<string> httpStorageHeaderNameArray = new List<string>(); 
    Dictionary<string, string> ht = new Dictionary<string, string>(); 

    foreach (var key in hdrCollection) 
    { 
     if (key.Key.ToLowerInvariant().StartsWith("x-ms-", StringComparison.Ordinal)) 
     { 
      if (ht.ContainsKey(key.Key.ToLowerInvariant())) 
      { 
       ht[key.Key.ToLowerInvariant()] = string.Format("{0},{1}", ht[key.Key.ToLowerInvariant()], 
        hdrCollection.FirstOrDefault(m => m.Key == key.Key).ToString().Replace("\n", string.Empty).Replace("\r", string.Empty).Trim()); 
      } 
      else 
      { 
       httpStorageHeaderNameArray.Add(key.Key.ToLowerInvariant()); 
       ht.Add(key.Key.ToLowerInvariant(), 
       hdrCollection.FirstOrDefault(m => m.Key == key.Key).Value.FirstOrDefault().ToString().Replace("\n", string.Empty).Replace("\r", string.Empty).Trim()); 
      } 
     } 
    } 

    httpStorageHeaderNameArray.Sort();// Now go through each header's values in the sorted order and append them to the canonicalized string. 
    foreach (string key in httpStorageHeaderNameArray) 
    { 
     retVal.AppendFormat("{0}:{1}\n", key.Trim(), ht[key]); 
    } 
    return retVal.ToString(); 
} 

'

回答

2

最新的存储客户端库的版本支持WinRT的。你可以在这里阅读更多关于它的信息:http://blogs.msdn.com/b/windowsazurestorage/archive/2012/10/29/introducing-windows-azure-storage-client-library-2-0-for-net-and-windows-runtime.aspx。我所做的是从Github下载源代码:https://github.com/WindowsAzure/azure-sdk-for-net,在VS 2012中打开解决方案并构建RT项目以获取必需的winmd文件。

来到你的问题,我相信你遇到这个问题,因为你传递一个空字符串规范化的资源字符串:

string authValue = string.Format(fmtStringToSign, "GET", "", "", "", hdr, "") 

请参阅关于创建规范化的资源字符串此链接查看更多细节:http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx

+0

你是我的英雄。我真的不确定资源字符串应该是什么。真的,虽然winrt支持的提示是我想要的:)它将节省大量的工作。谢谢! – 2013-03-24 22:10:12