c# - Authorization Header for Windows Azure Storage REST API from Windows Store App -
so trying list tables in storage account test authorization using query tables method. tried using sdk, sdk trying reference dlls aren't available in rt. decided try out rest api. having trouble authentication spec 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)); // // stuff 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 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 missing continue 403's. see problems looking through code?
a few comments:
there's storage client library windows rt well. please take @ answer here: working azure in winrt rest api, trouble signature.
coming problem, can try changing following line of code:
headers["date"] = datetime.now.tostring("r"); to
headers["date"] = datetime.utcnow.tostring("r"); and see if helps.
update
i noticed you're using cryptographicbuffer.convertstringtobinary convert base64 encoded key bytes. please try using cryptographicbuffer.decodefrombase64string (http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.cryptographicbuffer.decodefrombase64string.aspx) instead.
Comments
Post a Comment