2

I am trying to login automatically into an ASP.NET website using NSMutableURLRequest. I am sending through all the post parameters I can think of, and it works well with other websites. (eg. Wordpress based). But I am unable to get it to work for ASP.net websites. Can someone please explain to me how I could go about this issue?

NSString *post = @"__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUJNjM4NDU2MzY5ZGT%2F7rz73weImm5JbYQQ4q2lRY3HUw%3D%3D&__EVENTVALIDATION=%2FwEWBQK9gI6LAwKY4eTfDQKmw8%2B%2FAQLerrfAAwKQxuCGAT0xfZqPEPwFCfa5fbrvTZXDSnbY&ctl00%24SideBarContent%24UserName=demo&ctl00%24SideBarContent%24Password=demo&ctl00%24SideBarContent%24LoginButton=Login&ctl00%24SideBarContent%24UserTimeZoneCrawler%24offset=02%3A00%2C0";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *postRequest = [[NSMutableURLRequest alloc] init];
[postRequest setURL:[NSURL URLWithString:@"http://asp.net/website/logon.aspx"]];
[postRequest setHTTPMethod:@"POST"];
[postRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[postRequest setHTTPBody:postData];

[postRequest setValue:@"loginCookie=UserName=demo; SessionContext=ydnwyyz5n50mt2zjn3yiarq0" forHTTPHeaderField:@"Cookie"];

Regards, EZFrag

EZFrag
  • 317
  • 12
  • 29
  • Check [NSURLConnection and Basic HTTP Authentication][1] [1]: http://stackoverflow.com/questions/1973325/nsurlconnection-and-basic-http-authentication – Iłya Bursov Oct 14 '13 at 06:12

2 Answers2

0

You might have to pass your username/password in with your URL.

[postRequest setURl:[NSURL URLWithString:@"http://<username>:<password>@asp.net/website/login.aspx"]];
Simon Germain
  • 6,834
  • 1
  • 27
  • 42
  • I believe that that method is used for "browser authentication". I tried it anyway, but no results. – EZFrag Feb 14 '12 at 12:45
0

I ended up with letting the users enter their usernames and passwords manually every time. I am still open to any suggestions though.

Regards, EZFrag

EZFrag
  • 317
  • 12
  • 29