首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过AngularJs发送AntiForgeryToken?

如何通过AngularJs发送AntiForgeryToken?
EN

Stack Overflow用户
提问于 2021-09-07 11:05:29
回答 2查看 100关注 0票数 0

在我的MVC项目中,我调用了AngularJs的method。我需要向method发送AntiForgeryToken

在视图中

代码语言:javascript
复制
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    ...
}

在MVC controller

代码语言:javascript
复制
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Model model)
{
  ...
}

在AngularJs controller

代码语言:javascript
复制
this.data.Name= $('#txtNm').val();
this.data.Id = $('#Id').val();
var token = angular.element("input[name='__RequestVerificationToken']").val();

$http({
method: "POST",
url: "/Students/Create",
dataType: 'json',
data: this.data,
headers: {
  '__RequestVerificationToken': token
 }
}).then(function (response) {
                //success
}, function (response) {
                //error
});

但是,它不起作用。

EN

回答 2

Stack Overflow用户

发布于 2021-09-07 11:41:30

尝尝这个

代码语言:javascript
复制
this.data.__RequestVerificationToken = $('input[name="`__RequestVerificationToken`"]').val();
.....

    data: this.data,
    dataType: 'JSON',
    contentType:'application/x-www-form-urlencoded; charset=utf-8',
.....
票数 0
EN

Stack Overflow用户

发布于 2021-09-07 14:18:50

1-CSHTML

令牌在服务器端通过调用AntiForgery.GetTokens方法生成。

代码语言:javascript
复制
<script>
            @functions{
        public string GetAntiForgeryToken()
        {
            string cookieToken, formToken;
            AntiForgery.GetTokens(null, out cookieToken, out formToken);
            return cookieToken + ":" + formToken;
        }


            }
    </script>

 @Html.AntiForgeryToken()
 <input name="RequestVerificationToken" data-ng-model="RequestVerificationToken" type="hidden" data-ng-init="RequestVerificationToken='@GetAntiForgeryToken()'" />

2-MVC控制器

处理请求时,从请求头部提取令牌。然后调用AntiForgery.Validate方法来验证令牌。如果令牌无效,Validate方法将引发异常。

为了验证AntiForgeryToken,我使用了MyValidateAntiForgeryTokenAttribute()。

代码语言:javascript
复制
[HttpPost]
        [MyValidateAntiForgeryTokenAttribute()]
        public ActionResult Create()
        {
            // Your students create logic will be here
        }

         [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
        public class MyValidateAntiForgeryTokenAttribute : FilterAttribute
        {
            private void ValidateRequestHeader(HttpRequestBase request)
            {
                string cookieToken = String.Empty;
                string formToken = String.Empty;
                string tokenValue = request.Headers["__RequestVerificationToken"];
                if (!String.IsNullOrEmpty(tokenValue))
                {
                    string[] tokens = tokenValue.Split(':');
                    if (tokens.Length == 2)
                    {
                        cookieToken = tokens[0].Trim();
                        formToken = tokens[1].Trim();
                    }
                }
                AntiForgery.Validate(cookieToken, formToken);
            }

3-角度JS控制器

在标题$scope.RequestVerificationToken中指定的http调用中

代码语言:javascript
复制
        '__RequestVerificationToken': $scope.RequestVerificationToken
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69087094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档