Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Can';无法在URLSessionLegate中接收NSURLAuthenticationMethodHTTPBasic_Ios_Swift_Basic Authentication_Credentials_Urlsession - Fatal编程技术网

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的远程服务器的连接时,允许应用程序验证服务器的 证书链
当您切换到https时,第二个条件被满足,因此方法被调用

有两种类型的身份验证挑战:会话级别和非会话级别。 对于非会话级别的挑战,URLSession调用
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的远程服务器的连接时,允许应用程序验证服务器的 证书链
当您切换到https时,第二个条件被满足,因此方法被调用

有两种类型的身份验证挑战:会话级别和非会话级别。 对于非会话级别的挑战,URLSession调用
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")
    }
}
艾尔