我正在尝试将我登录到使用基本格式验证方法的网站。
以下是表单:
<form id="login-form" class="body" action="login" method="post">
<div class="form-group label-floating">
<label class="control-label" for="username">Identifiant</label>
<input id="username" type="text" name="username" class="form-control" required="">
</div>
<div class="form-group label-floating">
<label class="control-label" for="password">Mot de passe</label>
<input id="password" type="password" name="password" class="form-control" required="">
</div>
<div class="footer">
<div class="col-md-offset-3 col-md-6">
<input type="hidden" id="csrf_token" name="_csrf" value="6e35c328-a6e5-47ce-886e-16d0d1e71bd7">
<button type="submit" class="btn btn-lg btn-primary btn-block">
Connexion
</button>
</div>
</div>
</form>
这是我的代码:
func login(user: String, password: String, token: String) {
let parameters = [ "username" : user, "password" : password, "_csrf" : token]
var headers = ["X-XSRF-TOKEN" : token, "Content-Type": "application/json"]
if let authorizationHeader = Request.authorizationHeader(user: user, password: password) {
headers[authorizationHeader.key] = authorizationHeader.value
}
let url = "https://auth.exemple.fr/login"
Alamofire.request(url, method: .post, parameters: parameters)
.responseJSON { (response) in
switch response.result {
case .success:
print(response.description)
break
case .failure(let error):
print(error)
}
}
}
正如您所看到的,网站api正在使用CSRF令牌,我可以通过另一个请求检索该令牌。然后,我应该在头/参数中使用此标记记录用户。但是我从服务器得到了这个回复:
SUCCESS: {
error = "Internal Server Error";
message = "No message available";
path = "/uaa/login";
status = 500;
timestamp = 1518704475137;
}
我很久以前就已经这样做了,原因丢失了我的代码。任何的想法?在此先感谢您的帮助。