2012-08-16 47 views
4

我在iOS編程中遇到了一些認證問題。我有一個在Windows 2003上完全針對IIS6的代碼,但在Windows Server 2008上無法使用IIS7。兩臺服務器上的安全選項相同(不提供匿名訪問和「集成Windows身份驗證」)。針對IIS7和Windows 2008的iOS「didReceiveAuthenticationChallenge」

這裏是 「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 "[email protected]" 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]; 
} 

}

+0

'didReceiveAuthenticationChallenge'方法是否被調用? – 2012-08-16 12:55:36

+0

是的,它叫,我可以在那裏放一些斷點(而代碼工作形式W2003服務器) – malamirada 2012-08-16 14:06:37

+0

「不幸」我沒有看到任何與你的iOS方法不尋常的東西。任何機會調查挑戰響應是否回到服務器,如果有,那裏會發生什麼? – 2012-08-16 14:53:01

回答

2

最後,我發現的bug ...問題是相關的驗證方法在Windows 2008服務器IIS7 。

當您使用「集成Windows身份驗證」時,服務器可以使用NTLM或Kerberos。即使Kerberos未在這些機器上配置,我的2008服務器也始終使用kerberos。

該解決方案是編輯IIS元數據庫以強制NTML身份驗證。