本文介绍了Alamofire认证无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Alamofire
进行简单的请求
I'm using Alamofire
to do a simple request
Alamofire.request(.GET, URL)
.authenticate(user: user, password: password)
.responseJSON { response in
...
}
在收到第一个有效请求之后,我更改了带有无效证书的凭据,请求成功了,但应该失败。
After a first valid request, I changed the credential with invalid ones and the request succeed, but it should fail.
如何使以前的凭据无效?
How can I invalidate previous credentials?
请求成功后,如果我更改凭据,则 Alamofire
对先前的凭据进行身份验证。
After a successful request, if I change the credential, Alamofire
authenticates the previous credential.
如何使以前的凭据无效?
How can I invalidate previous credentials?
推荐答案
添加授权标头
let user = "user"
let password = "password"
let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.request(.GET, "https://httpbin.org/basic-auth/user/password", headers: headers)
.responseJSON { response in
debugPrint(response)
}
这篇关于Alamofire认证无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!