objective-c - 带有客户端证书和 NTLM 的 NSURLConnection

标签 objective-c nsurlconnection ssl-certificate ntlm

我正在尝试访问受 NTLM 身份验证保护并需要客户端证书的服务器。我正在使用 NSURLConnection 的委托(delegate)方法进行身份验证,并使用 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];
}

当服务器分别需要 NTLM 身份验证或客户端证书时,一切正常。当同时需要时,服务器端会同时接收证书信息和 NTLM 凭据,但 IIS7 会返回一个 403 页面,要求提供客户端证书...

您可能需要知道 willSendRequestForAuthenticationChallenge 按以下顺序调用了四次:

willSendRequestForAuthenticationChallenge: NSURLAuthenticationMethodServerTrust
willSendRequestForAuthenticationChallenge: NSURLAuthenticationMethodClientCertificate
willSendRequestForAuthenticationChallenge: NSURLAuthenticationMethodNTLM
willSendRequestForAuthenticationChallenge: NSURLAuthenticationMethodClientCertificate

如果你有什么想法?

提前致谢

最佳答案

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

关于objective-c - 带有客户端证书和 NTLM 的 NSURLConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15877257/

相关文章:

ios - 在 iPhone 上播放直播视频

ios - 警报 View 中的响应

java - 为即将到来的 SSL 调用配置 WebSphere 服务器的正确方法

ssl - 使用 tomcat 设置 SSL(自签名证书)

azure - 如何从 Azure 导入证书?

objective-c - iOS:将两个 NSMutableArray 存储在一个 .plist 文件中

c++ - 旋转二维矩形相交检测

ios - scrollView 放大时无法选择 UIView

concurrency - 异步 NSURLConnection,并发 NSOperation,什么时候使用 NSRunLoop?

php - 我可以在 iOS 设备上运行 PHP 脚本吗?