文章内容

2017/3/4 9:15:51,作 者: 黄兵

所需的防伪表单字段“__RequestVerificationToken”不存在

最近在写代码的时候提示:所需的防伪表单字段“__RequestVerificationToken”不存在。

解决方法如下:

@using (Html.BeginForm("AddArticle""Manage", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                @Html.AntiForgeryToken()
                <div class="ibox-content">
                    <div class="form-group">
                        @Html.LabelFor(model => model.Title, htmlAttributes: new { @class "control-label col-md-2" })
                        <div class="col-sm-10">
                            @Html.TextBoxFor(model => model.Title, new { @class "form-control", required = "", maxlength = "200" })
                            @Html.ValidationMessageFor(model => model.Title, ""new { @class "text-danger" })
                        </div>
                    </div>
                    @*<div class="hr-line-dashed"></div>
                    <div class="form-group">
                        @Html.LabelFor(model => model.AuthorId, htmlAttributes: new { @class "control-label col-md-2" })
                        <div class="col-sm-10">
                            @Html.DropDownList("AuthorId", ViewData["authories"as SelectList, htmlAttributes: new { @class "form-control" })
                            @Html.ValidationMessageFor(model => model.AuthorId, ""new { @class "text-danger" })
                        </div>
                    </div>*@
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        @Html.LabelFor(model => model.Keyword, htmlAttributes: new { @class "control-label col-md-2" })
                        <div class="col-sm-10">
                            @Html.TextBoxFor(model => model.Keyword, new { @class "form-control", maxlength = "100" })
                            @Html.ValidationMessageFor(model => model.Keyword, ""new { @class "text-danger" })
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        @Html.LabelFor(model => model.Description, htmlAttributes: new { @class "control-label col-md-2" })
                        <div class="col-sm-10">
                            @Html.TextBoxFor(model => model.Description, new { @class "form-control", maxlength = "200" })
                            @Html.ValidationMessageFor(model => model.Description, ""new { @class "text-danger" })
                        </div>
                    </div>                  
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        @Html.LabelFor(model => model.Body, htmlAttributes: new { @class "control-label col-md-2" })
                        <div class="col-sm-10">
                            <div class="ibox float-e-margins">
                                <div class="ibox-title">
                                    <h5>文章内容</h5>
                                    <div class="ibox-tools">
                                        <a class="collapse-link">
                                            <i class="fa fa-chevron-up"></i>
                                        </a>
                                        <a class="dropdown-toggle" data-toggle="dropdown" href="form_editors.html#">
                                            <i class="fa fa-wrench"></i>
                                        </a>
                                        <ul class="dropdown-menu dropdown-user">
                                            <li>
                                                <a href="form_editors.html#">选项1</a>
                                            </li>
                                            <li>
                                                <a href="form_editors.html#">选项2</a>
                                            </li>
                                        </ul>
                                        <a class="close-link">
                                            <i class="fa fa-times"></i>
                                        </a>
                                    </div>
                                </div>
                                <div class="ibox-content no-padding">
                                    @Html.TextAreaFor(model => model.Body, new { @class "form-control note-codable summernote" })
                                    @Html.ValidationMessageFor(model => model.Body, ""new { @class "text-danger" })
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <div class="col-sm-4 col-sm-offset-2">
                            <button class="btn btn-primary" type="submit">保存内容</button>
                        </div>
                    </div>
                </div>
            }

增加一个:@Html.AntiForgeryToken()

 // POST: /Manage/RemoveLogin
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> RemoveLogin(string loginProvider, string providerKey)
        {
            ManageMessageId? message;
            var result = await UserManager.RemoveLoginAsync(User.Identity.GetUserId(), new UserLoginInfo(loginProvider, providerKey));
            if (result.Succeeded)
            {
                var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
                if (user != null)
                {
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                }
                message = ManageMessageId.RemoveLoginSuccess;
            }
            else
            {
                message = ManageMessageId.Error;
            }
            return RedirectToAction("ManageLogins"new { Message = message });
        }      

后台增加一个:[ValidateAntiForgeryToken]
如果是[HttpGet]无效,必须是[HttpPost]。

分享到:

发表评论

评论列表