Ios Can';无法在URLSessionLegate中接收NSURLAuthenticationMethodHTTPBasic
我正在尝试使用Ios Can';无法在URLSessionLegate中接收NSURLAuthenticationMethodHTTPBasic,ios,swift,basic-authentication,credentials,urlsession,Ios,Swift,Basic Authentication,Credentials,Urlsession,我正在尝试使用urlsessionelegate和URLCredentials实现基本身份验证 func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void) 苹果建议不要手动写入授
urlsessionelegate
和URLCredentials
实现基本身份验证
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void)
苹果建议不要手动写入授权
标题。所以我尝试使用urlcidentifications
我在Swift游乐场做了一个非常简单的例子:
class Callback: NSObject {}
extension Callback: URLSessionDelegate {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void) {
print("received challenge")
print(challenge.protectionSpace.authenticationMethod)
completionHandler(.performDefaultHandling, nil)
}
}
let callback = Callback()
var configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration, delegate: callback, delegateQueue: nil)
let string = "http://demo0230490.mockable.io/test"
var request = URLRequest(url: URL(string: string)!)
session.dataTask(with: request) { (data, response, error) in
print("response")
print(response)
}.resume()
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
问题是–我没有收到验证挑战
如果您转到http://demo0230490.mockable.io/test
在浏览器中,您将看到浏览器显示基本身份验证警报。
从我的模拟http返回如下标题:
"Www-Authenticate" = (
"Basic realm=\"Access to MRM\""
);
我还试着切换到https(我的意思是https://demo0230490.mockable.io/test
)以检查是否接收类型为nsurAuthenticationMethodServerTrust
的身份验证质询。
这就行了。这意味着我的委托
已正确设置,并在某些情况下工作
所以问题是:
任何人都可以使用
urlcidentifications
(例如使用)来指出问题或提供基本身份验证的工作示例吗?您应该使用URLSessionTaskDelegate
的urlSession(uuuquo:task:didReceive:completionHandler:)
。您使用的方法用于连接级别的质询,而不是应用级别的质询。您应该使用urlSession(uU2;:task:didReceive:completionHandler:)
的URLSessionAskdelegate
。您使用的方法用于连接级别的质询,而不是应用级别的质询。根据urlSession(\uuux0:didReceive:completionHandler:)
仅在两种情况下调用:
- 当远程服务器请求客户端证书或Windows NT LAN Manager(NTLM)身份验证时,允许您的应用程序提供 适当的证书
- 会话首次建立到使用SSL或TLS的远程服务器的连接时,允许应用程序验证服务器的 证书链
URLSession(uquot:task:didReceive:completionHandler:)
。对于会话级别的挑战,URLSession调用URLSession(\uuuIdReceive:completionHandler:)
会话级挑战和非会话级挑战之间的区别在urlSession(uquot:task:didReceive:completionHandler:)的讨论部分中进行了描述。
:
- 对于会话级挑战NSURAuthenticationMethodNTLM、NSURAuthenticationMethodGoverage、, NSURLAuthenticationMethodClientCertificate,或 NSURLAuthenticationMethodServerTrust NSURLSession对象调用 会话委托的urlSession(:didReceive:completionHandler:)方法。 如果您的应用程序不提供会话委托方法,则 NSURLSession对象调用任务委托的 urlSession(:task:didReceive:completionHandler:)方法来处理 挑战
- 对于非会话级别的质询(所有其他质询),URLSession对象调用会话委托的 urlSession(:task:didReceive:completionHandler:)方法来处理 挑战。如果您的应用程序提供了会话代理,并且您需要 处理身份验证,则必须处理身份验证 或提供调用 每个会话处理程序显式。会话代表的 未为调用urlSession(:didReceive:completionHandler:)方法 非会议一级的挑战
extension Callback: URLSessionTaskDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
print("received task challenge")
}
}
另外还有一个关于的工作示例。根据urlSession(\uuuyDidReceive:completionHandler:)
仅在两种情况下调用:
- 当远程服务器请求客户端证书或Windows NT LAN Manager(NTLM)身份验证时,允许您的应用程序提供 适当的证书
- 会话首次建立到使用SSL或TLS的远程服务器的连接时,允许应用程序验证服务器的 证书链
URLSession(uquot:task:didReceive:completionHandler:)
。对于会话级别的挑战,URLSession调用URLSession(\uuuIdReceive:completionHandler:)
会话级挑战和非会话级挑战之间的区别在urlSession(uquot:task:didReceive:completionHandler:)的讨论部分中进行了描述。
:
- 对于会话级挑战NSURAuthenticationMethodNTLM、NSURAuthenticationMethodGoverage、, NSURLAuthenticationMethodClientCertificate,或 NSURLAuthenticationMethodServerTrust NSURLSession对象调用 会话委托的urlSession(:didReceive:completionHandler:)方法。 如果您的应用程序不提供会话委托方法,则 NSURLSession对象调用任务委托的 urlSession(:task:didReceive:completionHandler:)方法来处理 挑战
- 对于非会话级别的质询(所有其他质询),URLSession对象调用会话委托的 urlSession(:task:didReceive:completionHandler:)方法来处理 挑战。如果您的应用程序提供了会话代理,并且您需要 处理身份验证,则必须处理身份验证 或提供调用 每个会话处理程序显式。会话代表的 未为调用urlSession(:didReceive:completionHandler:)方法 非会议一级的挑战
extension Callback: URLSessionTaskDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
print("received task challenge")
}
}
艾尔