c# - HttpClient does not see "Set-Cookie" header -


i have mvc login form:

[httppost]     public actionresult login(loginviewmodel viewmodel)     {         if (viewmodel.username == "alex" && viewmodel.password == "password")         {             formsauthentication.setauthcookie(viewmodel.username, false);             return redirecttoaction("index", "home");         }          return view();     } 

and console app client consumes mvc login action:

private static void getperson()     {         console.writeline("username:");         string username = console.readline();          console.writeline("password:");         string password = console.readline();          using (httpclient httpclient = new httpclient())         {             httprequestmessage authrequest = new httprequestmessage();             authrequest.method = httpmethod.post;             authrequest.requesturi = new uri(@"http://localhost:4391/account/login");             authrequest.content = new formurlencodedcontent(new list<keyvaluepair<string, string>>             {                 new keyvaluepair<string, string>("username", username),                 new keyvaluepair<string, string>("password", password)             });              httpresponsemessage authresponse = httpclient.sendasync(authrequest).result;             ienumerable<string> cookievalues;             authresponse.headers.trygetvalues("set-cookie", out cookievalues);          }     } 

problem: authresponse.headers.trygetvalues("set-cookie", out cookievalues) not finding "set-cookie" header cookievalues comes out "null".

i ran fiddler login action in mvc , here got back:

http/1.1 302 found cache-control: private content-type: text/html; charset=utf-8 location: / server: microsoft-iis/8.0 x-aspnetmvc-version: 5.1 x-aspnet-version: 4.0.30319 set-cookie:   .aspxauth=a6a22208aeca1f3e25b43c834be058a5019f3a9c55aed099ffad5b0fe7b289ec2c3f87c157b0c1ed338d1df0a6469e6c5de8d9db7a99d54d992ea10f26424ba579c262b7cd247ca4193879e058a233b7a0bc98e10503440b79eb988239c43696; path=/; httponly x-sourcefiles: =?utf-8?b?yzpcdxnlcnncywxlegfuzgvyxgrvy3vtzw50c1x2axn1ywwgc3r1zglvidiwmtncuhjvamvjdhncrm9ybvzhbhvlc0f1dghxzwjbuelcrm9ybvzhbhvlc0f1dghxzwjbuelcqwnjb3vudfxmb2dpbg==?= x-powered-by: asp.net date: sat, 19 apr 2014 20:09:23 gmt content-length: 428 

fiddler shows i'm getting "set-cookie" why httpclient not see it?

figured out - mvc action send 2 responses 1 cookie , second 1 without cookie. httpclient shows latest response why not find "set cookie" header.

but!

it seems httpclient save cookie first response internally , on request secured page under same domain httpclient automatically applies saved cookie.

in example if i'll connect "localhost:4391/api/person/1" httpclient i'll unauthorized. goal go "localhost:4391/account/login" httpclient, log in, generated cookie go "localhost:4391/api/person/1" , send cookie mentioned before httpclient automatically , dont need extract cookie first request login page.

so code below works needed!

private static void getperson()     {         console.writeline("username:");         string username = console.readline();          console.writeline("password:");         string password = console.readline();          httpclient httpclient = new httpclient();          httprequestmessage authrequest = new httprequestmessage();         authrequest.method = httpmethod.post;         authrequest.requesturi = new uri(@"http://localhost:4391/account/login");         authrequest.content = new formurlencodedcontent(new list<keyvaluepair<string, string>>             {                 new keyvaluepair<string, string>("username", username),                 new keyvaluepair<string, string>("password", password)             });          httpresponsemessage authresponse = httpclient.sendasync(authrequest).result;          httprequestmessage request = new httprequestmessage(httpmethod.get, @"http://localhost:4391/api/person/1");         request.headers.accept.add(new mediatypewithqualityheadervalue("application/json"));          httpresponsemessage response = httpclient.sendasync(request).result;          if (!response.issuccessstatuscode)         {             console.writeline("username or password incorrect");             return;         }          response.content.readasasync<person>().continuewith((x) => {                 person person = x.result;                  console.writeline("first name: {0}", person.firstname);                 console.writeline("last name: {0}", person.lastname);             });     } 

but going catching cookie have been able accomplish if action in controller looked this:

[httppost]     public actionresult login(string username, string password)      {         if (username == password)          {             formsauthentication.setauthcookie(username, true);             return new httpstatuscoderesult(httpstatuscode.ok);         }         return new httpunauthorizedresult();     } 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -