【问题标题】:Pass RequestVerificationToken from Angular js to mvc controller将 RequestVerificationToken 从 Angular js 传递给 mvc 控制器
【发布时间】:2015-02-03 07:05:11
【问题描述】:

我想将主页中的 RequestVerificationToken 传递给使用 ValidateAntiforgeryToken 属性修饰的控制器操作。

我的基本观点如下:

@{
    Layout = null;
}
@Html.AntiForgeryToken()

在 html 中呈现的 token 为:

<input name="__RequestVerificationToken" type="hidden" value="9DLRgZ1UYKCRdDxhIx0qJ9fovUJafQ8tvfkd21M6hJHQBRnbvNLu5BlYwZXwGUUXmkGfmB5cFMsgaH0rbd7OorW9WVC3XvQYGdbki3KoxMaYxfEf7FLELnm3IDF95bjET83Dls1ZnLNAoLxFO_2SbPkwg7lJjKF6F4vPWredPYM1" class="ng-scope">

我正在尝试在 httpExecute 函数中传递令牌,如图所示:

this.httpExecute = function (opt) {
$http.defaults.headers.common['_RequestVerificationToken'] = $(':input:hidden[name*="RequestVerificationToken"]').val();        
    return $http({
        method: opt.method,
        url: opt.url,
        params: opt.params,
        data: opt.data
    })
     .error(function (response) {
         if (response.IsTokenExpired == true) {
             $user.logoutsession();
         }
     });
}

但我将令牌值设为空/未定义。 我在这里做错了什么? 请帮忙。 谢谢

【问题讨论】:

    标签: asp.net-mvc angularjs angularjs-scope antiforgerytoken csrf-protection


    【解决方案1】:

    是的,我没看错,如果我像下面这样传递它:

    return $http({
            method: opt.method,
            url: opt.url,
            params: opt.params,
            data: opt.data,
            headers: {
                '__RequestVerificationToken': $(':input:hidden[name*="RequestVerificationToken"]').val()
            }
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多