我在iOS编程中遇到了一些身份验证问题.我有一个代码在
Windows 2003上完美地对抗IIS6,但在带有IIS7的Windows Server 2008上不起作用.两台服务器上的安全选项相同(无匿名访问和“集成Windows身份验证”).
这是“didReceiveAuthenticationChallenge”委托的代码:
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge { //USE STORED CREDENTIALS Credentials* cred = [[Credentials alloc] init]; NSString* userName = cred.userName; NSString* pass = cred.pass; NSString* authMethod = [[challenge protectionSpace] authenticationMethod]; //Kerberos (Negotiate) needs "user@realm" as username //NTLM Needs domain\\username if ([authMethod isEqualToString:NSURLAuthenticationMethodNTLM]) { userName = [NSString stringWithFormat:@"%@%@",@"es\\",userName]; } if ([authMethod isEqualToString:NSURLAuthenticationMethodNegotiate]) { userName = [NSString stringWithFormat:@"%@%@",userName,@"@subdomain.domain.com"]; } NSLog(@"Auth method in use: %@",authMethod); NSLog(@"User: %@",userName); NSLog(@"Pass: %@",pass); if ([challenge prevIoUsFailureCount] <= 1) { NSLog(@"received authentication challenge"); NSURLCredential *credential; credential = [NSURLCredential credentialWithUser:userName password:pass persistence:NSURLCredentialPersistenceForSession]; [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; } else { NSLog(@"Authentication error"); NSLog(@"Failed login with status code: %d",[(NSHTTPURLResponse*)[challenge failureResponse]statusCode]); [[challenge sender] cancelAuthenticationChallenge:challenge]; }
}