ios - How to correctly include .ASPXAUTH and ASP.NET_SessionId cookies with NSMutableURLRequest -
i've been @ problem few days. app needs login asp.net (version: 4.0.30319, mvc version: 3.0) server , post via nsmutableurlrequest restricted page (a page requires logged in access).
currently can, successfully, login website. login use code:
nsstring *post =[[nsstring alloc] initwithformat:@"loginemail=%@&password=%@",@"username@website.com",@"password"]; nsurl *url=[nsurl urlwithstring:@"http://www.website.com/login"]; nsdata *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes]; nsstring *postlength = [nsstring stringwithformat:@"%d", [postdata length]]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; [request seturl:url]; [request sethttpmethod:@"post"]; [request setvalue:postlength forhttpheaderfield:@"content-length"]; [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"]; [request sethttpbody:postdata]; nserror *error = [[nserror alloc] init]; nshttpurlresponse *responsemeta = nil; nsdata *urldata = [nsurlconnection sendsynchronousrequest:request returningresponse:&responsemeta error:&error];
i can check , see both asp.net_sessionid , .aspxauth cookie, website, can found in sharedhttpcookiestorage. understand both of these need sent request if want "be logged in".
the code checking cookies here:
nsarray *cookiesforurl = [cookiejar cookiesforurl: [nsurl urlwithstring: @"http://www.example.com"]]; (cookie in cookiesforurl) { if([cookie.name compare:@"asp.net_sessionid"] == nsorderedsame) { aspdotnet_sessionid = [cookie value]; nslog(@"asp.net_sessionid: %@" , aspdotnet_sessionid); } if([cookie.name compare:@".aspxauth"] == nsorderedsame) { dotaspxauth = [cookie value]; nslog(@".aspxauth: %@" , dotaspxauth); } }
to clear cookies use this:
nshttpcookiestorage *cookiestorage = [nshttpcookiestorage sharedhttpcookiestorage]; (nshttpcookie *each in [[cookiestorage cookiesforurl:[nsurl urlwithstring:@"http://www.example.com"]] copy]) { [cookiestorage deletecookie:each]; }
i tried posting restricted page modifying nsstring *post
, nsurl *url
, using same code above.
nsstring *post =[[nsstring alloc] initwithformat:[messageid stringbyappendingstring:@"=%@"],comment]; nsurl *url=[nsurl urlwithstring:@"https://www.website.com/message
this unsuccessful. assume because neglected cookies in way. have tried adding in code under [request sethttpmethod:@"post"];
:
[request addvalue:aspdotnet_sessionid forhttpheaderfield:@"cookie"];
and/or:
[request addvalue:dotaspxauth forhttpheaderfield:@"cookie"];
where aspdotnet_sessionid
, dotaspxauth
cookie values retrieved sharedhttpcookiestorage.
i feel simple, help?
after more testing, appears wrong when thought had been unsuccessful authentication. appears either nsurlconnection
or nsmutableurlrequest
handle cookies me , not need retrieve sharedhttpcookiestorage
or set them request.
further if were manually send cookies should of @ least format , not pure value because asp.net expects cookies in format:
[request setvalue:[nsstring stringwithformat:@"asp.net_sessionid=%@", aspdotnet_sessionid] forhttpheaderfield:@"cookie"]; [request setvalue:[nsstring stringwithformat:@".aspxauth=%@", dotaspxauth] forhttpheaderfield:@"cookie"];
or possibly if above not work:
[request setvalue:[[nsstring stringwithformat:@"asp.net_sessionid=%@", aspdotnet_sessionid] stringbyappendingstring:@"; path=/; httponly"] forhttpheaderfield:@"cookie"]; [request setvalue:[[nsstring stringwithformat:@".aspxauth=%@", dotaspxauth] stringbyappendingstring:@"; path=/; httponly"] forhttpheaderfield:@"cookie"];
where aspdotnet_sessionid
24 char session id , dotaspxauth
448 char authentication string.
Comments
Post a Comment