【问题标题】:Passing CSRF token in Alamofire request parameters在 Alamofire 请求参数中传递 CSRF 令牌
【发布时间】:2017-11-11 18:54:14
【问题描述】:

我想在我的网站上登录一个用户,该用户的登录表单中有一个 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"}

有什么想法吗?提前感谢您的帮助!

【问题讨论】:

    标签: ios swift http httprequest alamofire


    【解决方案1】:
    'null' was found on the request parameter '_csrf'
    

    我看到您正在使用“授权”名称作为参数,而 API 编译它从“_csrf”读取它将参数名称从“授权”更改为“_csrf”

    来自

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

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

    【讨论】:

      猜你喜欢
      • 2014-10-12
      • 2018-07-28
      • 2018-01-10
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 2015-04-08
      相关资源
      最近更新 更多