c# - Windows azure REST API to List containers issue -


i'm trying list containers in windows azure storage account. i'm struck exception
"the remote server returned error: (403) server failed authenticate request. make sure value of authorization header formed correctly including signature.."

but have included signature per instructions given, 1 find mistake in code ?

private static string signthis(string stringtosign,string  key,string  account)         {              string signature = string.empty;             byte[] unicodekey = convert.frombase64string(key);             using (hmacsha256 hmacsha256 = new hmacsha256(unicodekey))             {                 byte[] datatohmac = system.text.encoding.utf8.getbytes(stringtosign);                 signature = convert.tobase64string(hmacsha256.computehash(datatohmac));             }              string authorizationheader = string.format(                 system.globalization.cultureinfo.invariantculture,                 "{0} {1}:{2}",                 "sharedkey",                 account,                 signature);             return authorizationheader;         }         static void listcontainers()         {             thread.currentthread.currentculture = system.globalization.cultureinfo.invariantculture;             string key = @"mystorageaccountkey";             string account = @"mystorageaccountname";              datetime dt = datetime.utcnow;              string datastr = dt.tostring ("r",system.globalization.cultureinfo.invariantculture);             string stringtosign = string.format("get\n"                 + "\n" // content encoding                 + "\n" // content language                 + "\n" // content length                 + "\n" // content md5                 + "\n" // content type                 + "\n" // date                 + "\n" // if modified since                 + "\n" // if match                 + "\n" // if none match                 + "\n" // if unmodified since                 + "\n" // range                 + "x-ms-date:" + datastr + "\nx-ms-version:2014-02-14\n" // headers                 + "/{0}\ncomp:list", account);              string auth = signthis(stringtosign, key, account);             string method = "get";             string urlpath = string.format ("https://{0}.blob.core.windows.net/?comp=list", account);             uri uri = new uri(urlpath);             httpwebrequest reque = (httpwebrequest)webrequest.create(uri);             reque.method = method;             reque.headers.add("authorization", auth);             reque.headers.add("x-ms-date",datastr);             reque.headers.add("x-ms-version", "2014-02-14");              using (httpwebresponse response = (httpwebresponse) reque.getresponse ()) {                 using (streamreader reader = new streamreader(response.getresponsestream()))                 {                     string text = reader.readtoend();                 }             }         } 

edit : string used generate signature

get  x-ms-date:tue, 14 jul 2015 18:38:16 gmt x-ms-version:2014-02-14 /mystorageaccountname/ comp:list 

edit : received exception response :

<?xml version="1.0" encoding="utf-8"?><error><code>authenticationfailed</code><message>server failed authenticate request. make sure value of authorization header formed correctly including signature. requestid:2fc74ef8-0001-0083-2664-be8850000000 time:2015-07-14t18:38:18.0831721z</message><authenticationerrordetail>the mac signature found in http request '5rqwnl2i8kuzf6hacrqfr1s0viom9eljz4l/zu6gcsg=' not same computed signature. server used following string sign: 'get  x-ms-date:tue, 14 jul 2015 18:38:16 gmt x-ms-version:2014-02-14 /mystorageaccountname/ comp:list'.</authenticationerrordetail></error> 

final edit: after making changes specified gauvrav, found storagekey used wrong, after replacing right one, working fine.

there may other changes error: please refer link

please change stringtosign to:

        string stringtosign = string.format("get\n"             + "\n" // content encoding             + "\n" // content language             + "\n" // content length             + "\n" // content md5             + "\n" // content type             + "\n" // date             + "\n" // if modified since             + "\n" // if match             + "\n" // if none match             + "\n" // if unmodified since             + "\n" // range             + "x-ms-date:" + datastr + "\nx-ms-version:2014-02-14\n" // headers             + "/{0}/\ncomp:list", account);//notice "/" after "{0}" 

it missing / after account name placeholder (last line in code above). once that, should able see list of containers returned in xml format.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -