我正在使用Alamofire
来执行简单的请求
Alamofire.request(.GET, URL)
.authenticate(user: user, password: password)
.responseJSON { response in
...
}
在第一次有效请求之后,我使用无效的凭据更改了凭据并且请求成功,但它应该失败。
如何使以前的凭据无效?
成功请求后,如果我更改了凭据,Alamofire
会对先前的凭据进行身份验证。
如何使以前的凭据无效?
答案 0 :(得分:1)
我经历了完全相同的事情。 iOS的答案应该有效,无论如何我会使用swift3提供代码片段,以减少研究时间。
let user = "aaaaa"
let password = "myPassword"
let plainString = "\(user):\(password)"
let plainData = plainString.data(using: .utf8)
let base64String = plainData?.base64EncodedString(options:NSData.Base64EncodingOptions(rawValue: 0))
let authString = "Basic " + base64String!
// Set in in Authorize header like...
let headers: HTTPHeaders = [
"Authorization": authString,
"Accept": "application/json"
]
// And use it like this..
Alamofire.request("https://httpbin.org/headers", headers: headers).responseJSON { response in
debugPrint(response)
}
以下是讨论此案例的地方>> https://github.com/Alamofire/Alamofire/issues/32
答案 1 :(得分:0)
添加授权标题帮助了我。
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)
}