在WinRT中与Azure的工作与REST API,麻烦签名 [英] Working with Azure in Winrt with Rest API, trouble with signature

查看:182
本文介绍了在WinRT中与Azure的工作与REST API,麻烦签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WinRT的Azure存储工作。由于Azure存储客户端不与WinRT中,我试图用天蓝色的REST API兼容。我有一个时间赫克得到签名权,我可以用另一套的眼睛帮我看看,我要去哪里错了。

I'm trying to work with azure storage in winrt. Since the azure storage client is not compatible with winrt I am trying to use azure's rest API. I am having a heck of a time getting the signature right and I could use another set of eyes to help me see where I'm going wrong.

Azure的客户提供了一个名称和关键属性,这种方法现在建立了请求只是列出所有的斑点。

Azure Account provides a name and key property, this method builds up the request right now simply listing all blobs.

私人异步无效BuildHTT prequest(AzureAccount帐户)
    {
        System.Net.Http.HttpClient要求=新的HttpClient();

'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) { }


}

这应该设置标题为签约...

This should set the headers up for signing...

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();
}

推荐答案

最新版本的存储客户端库支持WinRT的。你可以阅读更多关于它在这里:<一href=\"http://blogs.msdn.com/b/windowsazurestorage/archive/2012/10/29/introducing-windows-azure-storage-client-library-2-0-for-net-and-windows-runtime.aspx\" rel=\"nofollow\">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源$ C ​​$ C: https://github.com / WindowsAzure / Azure的SDK换网,拉开了解决方案,VS 2012和内置 RT 项目,以获得必要的 winmd 文件。

Latest version of storage client library supports WinRT. You can read more about it here: http://blogs.msdn.com/b/windowsazurestorage/archive/2012/10/29/introducing-windows-azure-storage-client-library-2-0-for-net-and-windows-runtime.aspx. What I did was download the source code from Github: https://github.com/WindowsAzure/azure-sdk-for-net, opened the solution in VS 2012 and built RT project to get the necessary winmd files.

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

Coming to your problem, I believe you're running into this issue because you're passing an empty string for canonicalized resource string:

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

请参阅关于创建规范化的资源字符串此链接了解详情: HTTP ://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx

Please see this link for more details on creating canonicalized resource string: http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx.

这篇关于在WinRT中与Azure的工作与REST API,麻烦签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆