2

I am using the below code in iPhone application for authentication with the server over https.

Authentication is happening on the web browser,but when I am trying to authenticate from iPhone application, the server response code in 404. Using the below code, authentication is happening successfully from iPhone application over http.

So I want to know is there any major change for authentication over https in iOS 5.

My code is as below.

-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
   if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
   {
       if ([challenge.protectionSpace.host isEqualToString:MY_PROTECTION_SPACE_HOST])
       {
           [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
       }

       [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
   }
   else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic])
   {
      if ([challenge previousFailureCount] == 0)
      {
          NSURLCredential *newCredential;

          newCredential = [NSURLCredential credentialWithUser:@"username"
                        password:@"password"
                        persistence:NSURLCredentialPersistenceForSession];

          [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
      }
      else
      {
          [[challenge sender] cancelAuthenticationChallenge:challenge];


          NSLog (@"failed authentication");

         
      }
  }
}

Thanks in advance.

ramuka2012
  • 23
  • 5
  • 1
    The 404 or Not Found error message is a HTTP standard response code indicating that the client was able to communicate with the server, but the server could not find what was requested. So make sure you are making the request in right format. – Praveen-K Jul 10 '12 at 19:24
  • That would be my guess as well. 404 means that the https site probably isn't there. Have you tried connecting to it in a browser or with curl? Make sure you can get a response back outside of the app, then try it again. – Bill Burgess Jul 10 '12 at 19:26
  • I have tried in iPhone safari and able to access the page and able to authenticate.But not able to authenticate using iPhone application.When I tried with IP address then the server response code is 409. – ramuka2012 Jul 11 '12 at 03:10
  • so, this code used to work prior to iOS5? does this block of code get executed: 'if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])...'? – Maggie Jul 11 '12 at 07:23

1 Answers1

2

For authentication over https you can try this code. For more details you can download the source code from http://as.wiley.com/WileyCDA/WileyTitle/productCd-1119961327,descCd-DOWNLOAD.html and see the iHotelApp in that.

self.networkQueue = [ASINetworkQueue queue];
  [self.networkQueue setMaxConcurrentOperationCount:6];
  [self.networkQueue setDelegate:self];
  [self.networkQueue go];

  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:LOGIN_URL]];

    [request setUsername:loginName];
    [request setPassword:password]; 

  [request setDelegate:self];

    [request setDidFinishSelector:@selector(loginDone:)];
    [request setDidFailSelector:@selector(loginFailed:)];   

    [self.networkQueue addOperation:request];
User97693321
  • 3,336
  • 7
  • 45
  • 69
  • 2
    FYI, this code is now more easily accessed through GitHub: https://github.com/rnapier/ios5ptl. Also FYI, the ASI package was deprecated just as the book was going to press. We are recommending MKNetworkKit as a replacement (https://github.com/MugunthKumar/MKNetworkKit). See http://iosptl.com/posts/mknetworkkit/ for details. – Rob Napier Jul 11 '12 at 20:31