HTTP (NTLM) authentication with Cocoa
HTTP (NTLM) authentication with Cocoa
- Subject: HTTP (NTLM) authentication with Cocoa
- From: Niklas Saers <email@hidden>
- Date: Tue, 1 Jul 2008 09:00:24 +0200
Hi guys,
had a tip from Jens that macnetworkprog@ might be a better place for
my question than cocoa-dev.
I'm trying to do NTLM authentication from my client program that I
want to keep all-Cocoa. I've tried following the different tutorials I
could find on the net, but they're usually discussing Basic
authentication, and I don't seem to get even that right in my
code. ;-) What I suspect I'm missing is something that binds the
NSURLCredential to NSMutableURLRequest.
The userinfo from the error I get is this one:
{
NSErrorFailingURLKey = http://192.168.0.5:42334/;
NSErrorFailingURLStringKey = "http://192.168.0.5:42334/";
NSUnderlyingError = Error Domain=kCFErrorDomainCFNetwork
Code=-1012 UserInfo=0x1f0b40 "Operation could not be completed.
(kCFErrorDomainCFNetwork error -1012.)";
}
The HTTP header dialogue between my client and the server goes like
this:
GET /Pages/Default.aspx HTTP/1.1
User-Agent: (myApp/0.1 (myApp))
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: 192.168.0.5:42334
A 401 is passed back from the server.
HTTP/1.1 401 Unauthorized
Content-Length: 1656
Content-Type: text/html
Server: Microsoft-IIS/6.0
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
MicrosoftSharePointTeamServices: 12.0.0.4518
X-Powered-By: ASP.NET
Date: Mon, 30 Jun 2008 12:55:40 GMT
Then happens the weirdest thing: the client sends the exact same
request back again. It doesn't add the credentials at all. And of
course, the server sends the exact same information back again.
Jens informed me that I have to add the Domain for authentication, but
NSURLCredential doesn't mention Domain at all. I'm able to
authenticate just fine through Safari, and CFNetwork Programming Guide
has a bit on it, but I'd much prefer to keep living in the world of
Cocoa to ensure compability with Cocoa Touch.
Here is my code:
+ (void) test {
NSString *host = @"http://192.168.0.5:42334/";
NSString *username = @"someuser";
NSString *password = @"somepass";
int port = 42334;
NSURLCredential *newCredential =[NSURLCredential
credentialWithUser:username password:password
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc]
initWithHost:host port:port protocol:@"http" realm:nil
authenticationMethod:nil];
NSURLCredentialStorage *store = [NSURLCredentialStorage
sharedCredentialStorage];
[store setCredential:newCredential forProtectionSpace:space];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:host] cachePolicy:NSURLCacheStorageNotAllowed
timeoutInterval:30];
[urlRequest setValue:@"(myApp/0.1 (myApp))" forHTTPHeaderField:@"User-
Agent"];
NSURLResponse *response;
NSError *error;
NSData *returnData = [NSURLConnection
sendSynchronousRequest:urlRequest returningResponse:&response
error:&error];
if(error) {
NSLog(@"test error: %@", error);
NSLog(@"test userinfo: %@", [error userInfo]);
} else {
NSLog(@"test response: %@", response);
NSLog(@"test result: %d", returnData);
}
}
What am I doing wrong? Why is there not added an Authorization-field
to my HTTP request?
Cheers
Nik
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden