Friday, April 8, 2011

In the iPhone SDK, how would you programatically pass a username / password to a secure site

How can you, in the iPhone SDK, programatically access a password protected site that has been protected by .htaccess or access control.

Using NSURLRequest, you can make a GET request from a URL. But if the URL is protected how would you send the username password pair. Also, what if it's encrypted with standard MD5 encryption.

From stackoverflow
  • Add something like this to your HTTPConnectionDelegate:

    -(void)connection:(NSURLConnection *)aConnection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
            if ([challenge previousFailureCount] == 0) {
                    NSURLCredential *newCredential;
                    newCredential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
                    [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
            } else {
                    [[challenge sender] cancelAuthenticationChallenge:challenge];
            }
    }
    
    L. DPenha : Thanks..Is this passed as clear text? Some webservers reject clear text passwords.
    Marc Novakowski : The docs aren't clear, but I'd have to guess that it uses the authentication method requested by the server. If that's basic-auth, then it won't be encrypted (unless the connection is SSL). If it's digest auth, then it will use the md5 one-way hashing method.

0 comments:

Post a Comment