Jor*_*orn 5 basic-authentication ios nsurlsession swift
我有以下代码来验证MediaWiki服务器的iOS应用程序:
let task = NSURLSession.sharedSession()
task.configuration.HTTPAdditionalHeaders = ["Authorization": "Basic " + base64EncodedCredentials]
task.dataTaskWithURL(url!) {(data, response, error) in
dispatch_async(dispatch_get_main_queue(), {
if data != nil {
let result = NSString(data: data!, encoding: NSUTF8StringEncoding)
if resultIsValid(result) {
//Do authentication stuff
} else {
self.showLoginErrorMessage()
}
} else {
self.showLoginErrorMessage()
}
})
}.resume()
Run Code Online (Sandbox Code Playgroud)
在iOS 8上,这很完美,我收到了一个HTTP 200 OK响应.但是,在iOS 9上,我收到了401未授权.不幸的是,我无法访问服务器以查看它实际收到的内容,而且它位于内部网络上,因此我无法链接到服务器.它使用HTTP基本访问身份验证,因此我假设它应该与具有该身份验证类型的任何其他服务器相同.
从iOS 8到9的API是否有任何变化可能导致此类问题?例如,可以更改/删除/添加其他默认标头,如内容类型或用户代理吗?
编辑:在使用requests.in测试之后,我发现通过添加Content-Type,这是iOS 8中标题的一部分,但不是在iOS 9中.在iOS 8中,我仍然可以在不设置内容的情况下获取请求-Type,但它在iOS 9请求中不存在仍然很奇怪.
我终于弄清楚了!事实证明,在 iOS 9 中,NSURLSessionConfiguration 的 HTTPAdditionalHeaders 属性是只读的,对其进行的任何更改都不会反映在当前的 NSURLSession 中。最重要的是,四个标头Authorisation、Connection、Host和WWW-Authenticate无法修改。因此,在 iOS 9 中进行基本访问身份验证的唯一方法是使用 Quellish 提出的 NSURLCredential。
对于遇到同样问题的人,这是我用来让身份验证适用于 iOS 8 和 iOS 9 的最终代码:
let url = NSURL(string: "https://subdomain2.subdomain1.example.com")
let credential = NSURLCredential(user: username, password: password, persistence: NSURLCredentialPersistence.ForSession)
let protectionSpace = NSURLProtectionSpace(host: url!.host!, port: 443, `protocol`: url!.scheme, realm: "subdomain2.example.com", authenticationMethod: NSURLAuthenticationMethodHTTPBasic)
NSURLCredentialStorage.sharedCredentialStorage().setCredential(credential, forProtectionSpace: protectionSpace)
let task = NSURLSession.sharedSession()
task.dataTaskWithURL(url!) {(data, response, error) in
if data != nil {
if responseIsValid(response) {
//Do authenticated stuff
} else {
self.showLoginErrorMessage()
}
} else {
self.showLoginErrorMessage()
}
}.resume()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1065 次 |
| 最近记录: |