views:

65

answers:

1

Hello Stackoverflow,

So I am trying to log into my site using an NSMutableURLRequest which presents the credentials via a post. As a noobie, I have some questions about the functionality of this method to make sure I am understanding it.

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@&TARGET=%@",LoginId,LoginPwd,target];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setHTTPShouldHandleCookies:YES];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://somelogin.mycomp.verify.fcc"]]];
//[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

Now this will automatically handle any redirects via the delegate method correct? It will also collect any cookies along the way? Also, should I be using an asynchronous request instead? Thanks!

UPDATE: So I have deduced that some of the cookies are being ignored by the request and subsequent redirects. Does anyone know why that may be happening?

A: 

you can do something like this when logging in sychronously... the code is not perfect, but this is the general idea.

look here for documentation

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW1

// credentials set here
NSURLCredential *creds = [NSURLCredential credentialWithUser:LoginId password:LoginPwd
                                           persistence:NSURLCredentialPersistenceForSession];

NSURLProtectionSpace *protectionedSpace = [[NSURLProtectionSpace alloc]
  initWithHost:@"mycomp.verify.fcc"
  port:8443
  protocol:@"https"
  realm:nil
  authenticationMethod:nil];


[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:creds
                                            forProtectionSpace:protectionedSpace];

// removed credentials from line...
NSString *post = [NSString stringWithFormat:@"TARGET=%@",target];

here is a tutorial that does it asychronously

http://iphonedevelopertips.com/networking/handling-url-authentication-challenges-accessing-password-protected-servers.html

Aaron Saunders
Intresting, is it possible to change the cookie storage (security) policy? I feel like because it is authenticating on a different domain, the subsequent redirects are not collecting the cookies.
gabaum10
I have implemented a solution where I load up all of the credentials for the specific domains in the credential storage...so as URLs request credentials, they can retrieve them from the store
Aaron Saunders
right, well I don't have the option to use a challenge based authentication. I have to go through all the hoops to collect the cookies. It would be SO much easier if I had that option...
gabaum10
Figured it out, thanks for the response! I needed to modify my initial post with a better redirect. Now it works perfectly. Thanks again!
gabaum10