NSURLConnection与客户端证书和NTLM

rid*_*dan 6 ntlm objective-c ssl-certificate nsurlconnection

我正在尝试访问受NTLM身份验证保护并需要客户端证书的服务器.我正在使用NSURLConnection的委托方法进行身份验证,并使用UIWebview检索结果.

当服务器需要客户端证书时,我设法为NTLM身份验证和身份验证开发代码:

- (void) connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {    

    authMethod = challenge.protectionSpace.authenticationMethod;

    if ( [challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] )
    {
        [challenge.sender useCredential:[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust] forAuthenticationChallenge: challenge];
        return;
    }

    if ( [challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodClientCertificate] )
    {
        [... code to extract certificate ...]  
        NSURLCredential *credential = [NSURLCredential credentialWithIdentity:myIdentity certificates:(NSArray*)certsArray persistence:NSURLCredentialPersistencePermanent];
        [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
        return;
    }

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodNTLM])
    {
        NSURLCredential *credential;
        credential = [NSURLCredential
                      credentialWithUser:@"user"
                      password:@"password"
                      persistence:NSURLCredentialPersistencePermanent];
        [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
        return;
    }
    [[challenge sender] performDefaultHandlingForAuthenticationChallenge:challenge];
}
Run Code Online (Sandbox Code Playgroud)

当服务器单独需要NTLM身份验证或客户端证书时,一切正常.当需要在一起时,服务器端都会收到证书信息和NTLM凭据,但IIS7会返回403页面,要求提供客户端证书...

您可能需要知道的是willSendRequestForAuthenticationChallenge按此顺序调用四次:

willSendRequestForAuthenticationChallenge: NSURLAuthenticationMethodServerTrust
willSendRequestForAuthenticationChallenge: NSURLAuthenticationMethodClientCertificate
willSendRequestForAuthenticationChallenge: NSURLAuthenticationMethodNTLM
willSendRequestForAuthenticationChallenge: NSURLAuthenticationMethodClientCertificate
Run Code Online (Sandbox Code Playgroud)

如果您有任何想法?

提前致谢,

小智 1

该功能在 iOS 7 中有效,但在 iOS 8 中无效。您使用的是 iOS 8 吗?使用 iOS 7 进行测试(例如在模拟器上)以确认这只是 iOS 8 问题。它与您可能在日志窗口中看到的“流在打开之前发送事件”错误有关。也在等待它在 iOS 中修复,但我仍然在最新的 8.2 beta 3 中看到它。