AuthenticationMethodServerTrust 在 iOS 9 中为零

AuthenticationMethodServerTrust is nil in iOS 9

我一直在尝试使用 NSURLSession 在我的 iOS 8+ 应用程序中实现一种 public 键固定的方法。

我所做的只是实现委托方法 didReceiveChallenge 并获取 challenge.protectionSpace.authenticationMethod 并检查它是否等于 NSURLAuthenticationMethodServerTrust。

如果它等于 NSURLAuthenticationMethodServerTrust 我检查证书并将其与我的本地副本进行比较。

这在 iOS 8 上工作正常,但在 iOS 9 上我没有收到等于 NSURLAuthenticationMethodServerTrust 的身份验证方法。我收到 NSURLAuthenticationMethodClientCertificate,所以我无法访问 challenge.protectionSpace.serverTrust 属性.

有什么想法吗?

public func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void)
{


      if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust)
        {
            // Verify the identity of the server
                    println("Trusted")
                    return challenge.sender.performDefaultHandlingForAuthenticationChallenge!(challenge)

        }
        println("Not trusted")
        return challenge.sender.cancelAuthenticationChallenge(challenge)
    }
}

您的回调可以针对不同的保护 space 多次调用。这里发生的是这些调用的顺序发生了变化,服务器信任保护 space 调用显然由于某种原因没有首先发生 iOS 9.

问题是您正在取消第一个质询,这实际上终止了连接。不要那样做。相反,使用 "perform default handling".

您也可能不想对服务器信任案例执行默认处理,因为这等同于根本不编写自定义处理程序。相反,检查密钥,只有当它是您想要的密钥时才执行默认处理,否则取消身份验证质询。

或者我误读了您的代码。看起来好像缺少一些代码。可能是您根本没有处理非服务器信任案例,在这种情况下,这就是问题所在。如果您不为每个挑战调用 something,连接将永远停留在那里,等待您这样做。