Authentication 如何在urlsession中验证代理?
嗨,我正在尝试向我的应用程序添加带有凭据的代理,但每当我尝试时, 我收到以下(在Xcode中)控制台警告:Authentication 如何在urlsession中验证代理?,authentication,proxy,swift3,nsurlcredential,urlsession,Authentication,Proxy,Swift3,Nsurlcredential,Urlsession,嗨,我正在尝试向我的应用程序添加带有凭据的代理,但每当我尝试时, 我收到以下(在Xcode中)控制台警告: *** WARNING: CFMachPortSetInvalidationCallBack() called on a CFMachPort with a Mach port (0x900b) which does not have any send rights. This is not going to work. Callback function: 0x181bcf524
*** WARNING: CFMachPortSetInvalidationCallBack() called on a CFMachPort with a Mach port (0x900b) which does not have any send rights. This is not going to work. Callback function: 0x181bcf524
也得到了以下描述的警报
Authentication for HTTP proxy
MyProxyHost
MyProxyPort
当我取消弹出窗口时,它将调用以下方法
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
我在Xcode控制台中调试了该挑战,得到了以下调试说明:
po challenge.protectionSpace.debugDescription
"<NSURLProtectionSpace: 0x12f685ad0>: Host: MyProxyHost, Server:http, Auth-Scheme:NSURLAuthenticationMethodHTTPBasic, Realm:(null), Port: MyProxyPort, Proxy:YES, Proxy-Type:http"
从下面的字典中,我可以点击代理,但无法验证代理
// Create an NSURLSessionConfiguration that uses the proxy
let proxyDict: [AnyHashable: Any]? = [(kCFNetworkProxiesHTTPEnable as AnyHashable): Int(1), (kCFNetworkProxiesHTTPProxy as AnyHashable): proxyServerString, (kCFNetworkProxiesHTTPPort as AnyHashable): proxyPortNumber, (kCFProxyUsernameKey as AnyHashable): userNameKey, (kCFProxyPasswordKey as AnyHashable): password]
let configuration = URLSessionConfiguration.default
configuration.connectionProxyDictionary = proxyDict
我尝试了以下几种方法,但都失败了:
let loginString = String(format: "%@:%@", user, password)
let loginData = loginString.data(using: String.Encoding.utf8)!
let base64LoginString = loginData.base64EncodedString()// base64EncodedData(options: [])
configuration.httpAdditionalHeaders = ["Authorization" : base64LoginString]
另一种方式:
let protectionSpace:URLProtectionSpace = URLProtectionSpace(host: proxyHost, port: proxyPort, protocol: "http", realm: nil, authenticationMethod: NSURLAuthenticationMethodHTTPBasic)
let credentialStorage = URLCredentialStorage.shared//.allCredentials
credentialStorage.set(getURLCredential(), for: protectionSpace)
configuration.urlCredentialStorage = credentialStorage
另一种方式:
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
print("Challenge:")
guard challenge.previousFailureCount == 0 else {
print("Previous Failure Count = \(challenge.previousFailureCount)")
print("Cancelling Challenge\n")
challenge.sender?.cancel(challenge)
// Inform the user that the user name and password are incorrect
completionHandler(.cancelAuthenticationChallenge, nil)
return
}
print("Use Credential ....\n")
completionHandler(.useCredential, self.getURLCredential())
}
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if challenge.previousFailureCount > 0
{
print("Alert Please check the credential")
debugPrint("Alert Please check the credential")
completionHandler(.cancelAuthenticationChallenge, nil)
}
challenge.sender?.use(self.getURLCredential(), for: challenge)
completionHandler(.useCredential, self.getURLCredential())
}
无法进行身份验证
请建议我如何在swift 3中验证代理?您的每种方法都有不同的错误:
头用于向远程服务器而不是代理验证您自己。您需要设置Authorization
标题代理授权
- 您使用的kCFProxy*键在代理字典中无效(顺便说一句,这就是为什么对于设计未来API的人来说,使用字典作为数据结构是一种根本不好的设计)。这些键仅在配置
。您需要CF(读写)流时使用
和朋友。有关有效的代理字典示例,请参阅kcfstreampropertyhtpproxyhost
- 您实现了用于处理身份验证挑战的会话级委托方法。不幸的是,该方法不能用于代理身份验证。只有任务级委托方法(
)被调用以进行代理身份验证URLSession:task:…
let configuration.httpAdditionalHeaders = ["Proxy-Authorization": Request.authorizationHeader(user: "user", password: "password") ]
你找到答案了吗?我也面临同样的问题。您好,如果我设置
connectionProxyDictionary
,它是NSURLSessionConfiguration
的一部分。。。您知道它是否可以自动设置此标题(“代理授权”)吗?谢谢,我不知道@Zohar81。我构建了一个单独的URLSessionConfiguration
,并在每次需要代理自动化时重用它。
URLSessionConfiguration
let configuration.httpAdditionalHeaders = ["Proxy-Authorization": Request.authorizationHeader(user: "user", password: "password") ]