发布后,防伪表单字段__RequestVerificationToken不存在

时间:2016-04-15 16:00:53

标签: asp.net-mvc antiforgerytoken

我已获得注销链接:

<a id="lnkLogoff" href="javascript:document.getElementById('logoutForm').submit()"><i class="fa fa-lock"></i> @Resources.LogOff</a>

当按下链接时,它会提交我的注销表单:

<form method="post" action="@Url.Action("Logoff", "Account")" id="logoutForm">
    @Html.AntiForgeryToken()
</form>

99.9%的时间效果很好。但是当我提交表单时,我遇到了一个问题,它进入控制器并将模型返回到视图,显示结果上的烤箱信息。一旦它返回那里,我的所有AntiFOrgeryTokens似乎都缺失或无效,或者什么。

[HttpPost]
        [ValidateAntiForgeryToken]
        public PartialViewResult PersonUserOpen(UserPageModel model)
        {
            model.Function = DataFactory.SystemAccess.GetFunction(model.Function.Id);

            if (ModelState.IsValid && model.Roles.Any(a => a.IsGranted))
            {
                //This is valid, do stuff and add success message
            }
            else{
                 //add Error messages to be displayed
             }

             return PartialView(model);
}

再次显示视图及其消息后,所有表单的防伪标记都丢失或无效,不确定。因此,当我发布此视图的表单或单击注销以发布注销表单时,他们不会工作,因为它缺少防伪令牌。这有什么用处?此页面用于更新存储在UserManager中的用户密码,这可能与问题有关吗?在控制器的HttpGet方法中,我将要更改的用户添加到模型中:

var user = UserManager.FindById(person.ApplicationUserId);

                model.MembershipRoles = DataFactory.SystemAccess.GetMembershipRoles(user.Id);

                model.User = new UserViewModel
                {
                    Id = user.Id,
                    Email = user.Email,
                    Username = user.UserName,
                    Active = user.Active
                };

0 个答案:

没有答案