none
Authorization Header for Windows Azure Storage REST API from Windows Store App RRS feed

  • Question

  • So I am just trying to list the tables in the storage account to test the authorization using the Query Tables method. I tried using the SDK, but the SDK was trying to reference DLLs that aren't available in RT. Decided to try out the REST API. but am having trouble with the authentication from this

        public async Task ExecuteAsync()
        {
            try
            {
                HttpClient client = new HttpClient();
                Dictionary<string, string> headers = GetHeaders("accountname/Tables");
                client.DefaultRequestHeaders.Date = DateTimeOffset.Parse(headers["Date"]);
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("SharedKey", headers["Authorization"]);
                const string url = "azure_account_uri/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.UtcNow.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);
        }

    Obviously I am missing something as I continue to get 403's. See any problems looking through this code?

    Monday, April 8, 2013 4:44 PM

Answers

All replies