Objective-C and the proactive authentication issue


I am trying to get some WebServices data for my iPad application.

For this, I use NSURLConnection with NSMutableURLRequest.

Web services are published on Apache-Coyote 1.1 with Preemptive Basic Authentication, but I don’t know how to send my credentials from Objective C.

Does anyone know how I can set my user / password in Objetive-C to register my clients with Apache pre-authentication system?

Thanks.

+4
source share
2 answers

EDIT:

In order to set up your accounts yourself, you must be able to base64 encode the username and password and set the appropriate header. Matt Gallagher, from Cocoa Lovingly, has a great post on how to add a category to NSData to make it easy.

 NSString* username = @"username"; NSString* password = @"password"; NSString* encodedUsername = [[username dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString]; NSString* encodedPassword = [[password dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString]; NSURL* url = [NSURL URLWithString:@"http://yourUrl.com/"]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; NSString* headerValue = [NSString stringWithFormat:@"Basic %@:%@", encodedUsername, encodedPassowrd]; [request addValue:@"Authorization" forHTTPHeaderField:headerValue]; [NSURLConnection connectionWithRequest:request delegate:self]; 

As with credentials, make sure you do this all over HTTPS, because these credentials are essentially clear-text.

+2
source

Consider using the ASIHTTPRequest framework, which makes proactive basic authentication requests simple :

 NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/top_secret/"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic]; /** preemptively present credentials **/ [request setUsername:@"username"]; [request setPassword:@"password"]; [request startSynchronous]; NSError *error = [request error]; if (!error) NSString *response = [request responseString]; 

And yes, you definitely want to perform basic authentication over SSL (i.e., through some https://... URL).

0
source

All Articles