views:

1575

answers:

4

Hi All,

I have a web site hosted on IIS with windows authentication. I am trying to access it in one of my iPhone web application. Presently i am using this code, but it is not working.

NSString *authString = [[[NSString stringWithFormat:@"%@:%@", @"myusername", @"mypassword"]dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];

authString = [NSString stringWithFormat: @"Basic %@", authString];

**[requestObj setValue:authString forHTTPHeaderField:@"Authorization"];**

my web app is hosted with windows authentication. but here i am using basic. can any one post what is the correct http header for it.

Thanks..

+1  A: 

Windows authentication (NTLM) isn't as simple as basic authentication. NTLM requires more than one webrequest to negotiate the security so there isn't a static HTTP header you can send to log in.

David
do you have any idea about solving the problem...
nbojja
Switching to use basic authentication would be the easiest solution.
David
but my enterprise app is already hosted one...i cant chage the authentication type...
nbojja
A: 

You can use the third-party ASIHTTPRequest library to perform NTLM over HTTP authentication.

rpetrich
Hi...Can you post me some code how to do NTLM auth with ASIHTTPRequest.thanks..
nbojja
pokeb has beat me to it, please accept his answer :)
rpetrich
A: 

I'm not 100% sure it supports NTLM Authentication but have you investigated the connection:didReceiveAuthenticationChallenge method on the NSUrlConnection?

Lounges
yea...i checked didReceivedAuthenticationChallenge method. it is working fine to download data. but i am trying to play a video. with MPMoviePlayerController it is not working well.
nbojja
+1  A: 

I think the main difference is you need to specify the domain you are authenticating against as well as the username and password. Something like this should work. I've used a synchronous request for brevity, ideally you should use an ASINetworkQueue or NSOperationQueue to perform the request.

NSString *username = @"test";
NSString *password = @"test";
NSString *domain = @"test";
NSURL *url = [[[NSURL alloc] initWithString:@"http://myurl"] autorelease];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseSessionPersistance:YES];
[request setUsername:username];
[request setPassword:password];
[request setDomain:domain];
[request start];
if ([request error]) {
    if ([[request error] code] == ASIAuthenticationErrorType) {
     //Authentication failed
    }
} else {
    NSLog([request responseString]);
}

I don't have access to a Windows server to test this, but I have tested NTLM in the past so it should work... :)

pokeb
Thanks Pokeb...It is working fine...thaanks a lot....but i need to play a video from windows server. But MPMovieplayerController is not supporting any authentication mechanism. What to do???
nbojja
MPMoviePlayerController only supports loading media via the standard NSURL* methods. Your best bet is to add additional supported authentication types at the server, but as a last resort you could create a loopback HTTP server running on the device to proxy the data between NSURLConnection and the NTLM-authenticated server.
rpetrich