我一直在尝试使用NSURLSession在我的iOS 8+应用中实现公钥固定的方法。我所做的是实现代理方法didReceiveChallenge,获取challenge.protectionSpace.authenticationMethod并检查它是否等于NSURLAuthenticationMethodServerTrust。
如果它等于NSURLAuthenticationMethodServerTrust,我会检查证书并将其与本地副本进行比较。这在iOS 8上正常工作,但在iOS 9上,我没有收到一个等于NSURLAuthenticationMethodServerTrust的认证方法。我收到的是NSURLAuthenticationMethodClientCertificate,因此无法访问challenge.protectionSpace.serverTrust属性。
有任何想法吗?
如果它等于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)
}
}