在Alamofire请求参数中传递CSRF令牌

时间:2017-11-11 18:54:15

标签: ios swift http httprequest alamofire

我想在我的网站上记录一个用户,该用户在登录表单中有一个CSRF令牌。我在IOS上使用Alamofire和Swift。

以下是表单代码:

                   <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="7cf3b95d-1310-4d00-b1b2-7040340b12e6"/>
                                        <button type="submit" class="btn btn-lg btn-primary btn-block">
                                            Connexion
                                        </button>
                                    </div>
                                </div>
                            </form>

我创建了一个返回CSRF令牌的函数。然后我调用另一个函数,该函数使用他的凭据和CSRF令牌记录用户。

这是这个功能:

func login(token: String, completion: @escaping (_ success: Bool) -> Void) {

        let customerURL = "https://mywebsite.com/uaa/login"
        let user = "username"
        let password = "password"
        let parameters = [ "username" : user, "password" : password, "Authorization" : token]

        Alamofire.request(customerURL, method: .post, parameters: parameters, encoding: JSONEncoding.default)

            .responseString { response in

                switch response.result {
                case .success:
                    print("Validation Successful")
                    if let JSON = response.result.value {
                        print(JSON)
                    }
                case .failure(let error):
                    print(error)

                }
        }
    }

但是当我运行我的应用程序时,这就是我得到的:

{"timestamp":1510425797543,"status":403,"error":"Forbidden","message":"Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-XSRF-TOKEN'.","path":"/uaa/login"}

有什么想法吗?在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

'null' was found on the request parameter '_csrf'

我看到你正在使用参数的'授权'名称,而从'_csrf'读取它的API编译将参数的名称从'授权'更改为'_csrf'

let parameters = [ "username" : user, "password" : password, "Authorization" : token]

let parameters = [ "username" : user, "password" : password, "_csrf" : token]