我正在使用Alamofire
请求进行所有网络通话。最初,当我登录时,我使用一些USERNAME和PASSWORD对请求进行身份验证。比如USERNAME是JOHN,PASSWORD是1234。
说我立即退出并且我使用其他一些凭证登录,例如USERNAME是PETER,PASSWORD是4321.App再次使用旧凭证JOHN和1234登录。新值根本没有应用。
注销时,如何在注销时删除上一个会话?
添加了代码段
func login(userName: String!, password:String!, completion: (error: NSError?) -> Void) {
print(userName) // Prints PETER for second attempt
Alamofire.request(
.GET,
Management.LOGIN_URL)
.authenticate(user: userName , password: password)
.responseJSON { response in
// But gives JOHN's data for second attempt
switch response.result {
case .Success(let JSON):
print(JSON)
self.setCookies(response)
self.setUserData(JSON as! NSDictionary)
completion(error: error)
}
case .Failure(let error):
completion(error: error)
}
}
}
func logout() {
Alamofire.Manager.sharedInstance.session.resetWithCompletionHandler { () -> Void in
print("Resetted")
}
}
private func setCookies(response: Alamofire.Response<AnyObject, NSError>) {
if let
headerFields = response.response?.allHeaderFields as? [String: String],
URL = response.request?.URL
{
let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(headerFields, forURL: URL)
Alamofire.Manager.sharedInstance.session.configuration.HTTPCookieStorage?.setCookies(cookies, forURL: response.response!.URL!, mainDocumentURL: nil)
}
}
答案 0 :(得分: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)
}