Search code examples
iphoneiishttp-authentication

HTTP Authentication Headers for IIS windows authentication


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..


Solution

  • 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 URLWithString:@"http://myurl"];
    ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
    [request setUseSessionPersistence: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... :)