【发布时间】:2016-03-03 09:54:07
【问题描述】:
我一直在尝试使用 NSURLSession 在我的 iOS 8+ 应用中实现公钥固定。
我所做的只是实现委托方法 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)
}
}
【问题讨论】:
-
检查 stackoverflow.com/questions/33542003/… 链接。这可能会有所帮助。
-
嗨,Karlos,这对我不起作用。 :-( 我和维克多有同样的问题。
标签: ios swift authentication nsurlsession nsurlsessiontask