2012-04-08 8 views
5

Ich möchte, dass sich meine Anwendung gegen die Appliance authentifiziert, dh Benutzername und Passwort über https weiterleiten.IOS https-Authentifizierung

schaute ich auf einige Beispiele und im Grunde die folgenden Aufbau:

-(void)authentication 
{ 
    //setting the string of the url taking from appliance IP. 
    NSString *urlString =[NSString 
          stringWithFormat: 
          @"http://%@",appliance_IP.text]; 
    //define the url with the string 
    NSURL *url = [NSURL URLWithString:urlString]; 

    //creating request 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    //setting credential taking from the username and password field 
    NSURLCredential *credential = [NSURLCredential credentialWithUser:username.text password:password.text persistence:NSURLCredentialPersistenceForSession]; 


    NSURLProtectionSpace *protectionSpace= 
    [[NSURLProtectionSpace alloc]initWithHost:urlString port:443 protocol:@"https" realm:nil authenticationMethod:nil ]; 


    [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace]; 

    [ NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL]; 

} 

AS I Beispiele verwenden, die ich, dass ich etwas mehr Verständnis brauchen nahm, NSURLConnection in dem obigen Beispiel enthält nicht den Benutzernamen und das Passwort, wie ich tun hinzufügen? Die Anfrage enthält also auch einen Benutzernamen und ein Passwort. Vielleicht ist es besser, diese Informationen zu übergeben. Derzeit enthält die Anfrage nur die URL-Zeichenfolge.

Dank ER

Antwort

8

Wenn eine Anforderung eine Authentifizierung erfordern, erhalten Sie einen Rückruf an didReceiveAuthenticationChallenge erhalten, die Sie behandeln, wie folgt:

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(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:MY_USERNAME 
         password:MY_PASSWORD 
         persistence:NSURLCredentialPersistenceForSession]; 

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

      // inform the user that the user name and password 
      // in the preferences are incorrect 

      NSLog (@"failed authentication"); 

      // ...error will be handled by connection didFailWithError 
     } 
    } 
} 
0

Sie NSURLConnectionDelegate Delegat verwenden müssen NSURLConnection Ereignisse zu behandeln.

Zur Authentifizierung gibt es zwei Delegierte Methoden: connection:canAuthenticateAgainstProtectionSpace: und connection:didReceiveAuthenticationChallenge:

Siehe Beispiele in URL Loading System Programming Guide