1

所以我只是想列出存储帐户中的表来测试使用查询表方法的授权。我尝试使用 SDK,但 SDK 试图引用 RT 中不可用的 DLL。决定试用 REST API。但我无法通过此规范进行身份验证http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx

    public async Task ExecuteAsync()
    {
        try
        {
            HttpClient client = new HttpClient();
            Dictionary<string, string> headers = GetHeaders("/Tables");
            client.DefaultRequestHeaders.Date = DateTimeOffset.Parse(headers["Date"]);
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("SharedKey", headers["Authorization"]);
            const string url = "http://account-name.table.core.windows.net/Tables";
            XmlReader reader = XmlReader.Create(await client.GetStreamAsync(url));
            //
            // Do some stuff with the reader here
            //
        }
        catch (Exception e)
        {
            // handle exception
        }
    }

    public Dictionary<string, string> GetHeaders(string resource)
    {
        Dictionary<string, string> headers = new Dictionary<string, string>();
        headers["Date"] = DateTime.Now.ToString("R");
        headers["Authorization"] = GetAuthorizationHeader(resource, headers["Date"]);
        return headers;
    }

    public string GetAuthorizationHeader(string resource, string date)
    {
        const string key = PRIMARY_KEY;
        const string accountName = ACCOUNT_NAME;
        string signee = string.Join("\n", new List<string> { "GET", "", "", date, resource });
        // make the signature
        MacAlgorithmProvider hmac = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA256");
        IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
        CryptographicKey hmacKey = hmac.CreateKey(keyMaterial);
        IBuffer data = CryptographicBuffer.ConvertStringToBinary(signee, BinaryStringEncoding.Utf8);
        IBuffer hash = CryptographicEngine.Sign(hmacKey, data);
        string signature = CryptographicBuffer.EncodeToBase64String(hash);
        return string.Format("{0}:{1}", accountName, signature);
    }

显然,当我继续获得 403 时,我遗漏了一些东西。通过此代码查看任何问题?

4

1 回答 1

1

A few comments:

There's a storage client library for Windows RT as well. Please take a look at my answer here: Working with Azure in Winrt with Rest API, trouble with signature.

Coming to your problem, can you try changing the following line of code:

headers["Date"] = DateTime.Now.ToString("R");

to

headers["Date"] = DateTime.UtcNow.ToString("R");

and see if that helps.

UPDATE

I also noticed that you're using CryptographicBuffer.ConvertStringToBinary to convert Base64 encoded key to bytes. Please try using CryptographicBuffer.DecodeFromBase64String (http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.cryptographicbuffer.decodefrombase64string.aspx) instead.

于 2013-04-06T03:20:13.340 回答