MVC" LogOut"控制器导致"无法找到资源。"使用" ValidateAntiForgeryToken"时出错属性

时间:2014-04-22 12:31:54

标签: asp.net-mvc

当我将“ValidateAntiForgeryToken”属性添加到我的LoggOff控制器时,它不会映射我的控制器并引发“无法找到资源”。错误。问题是什么?这是我的控制器:

// POST
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    AuthenticationManager.SignOut();
    return RedirectToAction("Index", "Home");
}

以下是我的观点:

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id =    "__RequestVerificationToken", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>@Html.ActionLink("User: " + User.Identity.GetUserName(), "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })</li>
        <li>@Html.ActionLink("Log Off", "LogOff", "Account")</li>
    </ul>
    }
}
else
{
    <ul class="nav navbar-nav navbar-right">
        <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}

2 个答案:

答案 0 :(得分:1)

您在注销时未发布表单

此行将创建GET请求

<li>@Html.ActionLink("Log Off", "LogOff", "Account")</li>

您需要添加提交按钮,例如

<a href="javascript:document.getElementById('__RequestVerificationToken').submit()">Logout</a>

答案 1 :(得分:0)

测试此解决方案web.config,我遇到了这个问题,我忘记取消注释标记