17

我刚刚结束了一个大学项目,我不确定我是否已经盯着我的电脑太久并且遗漏了一些明显的东西,但是当我尝试注销用户时,我得到一个 404 不是找到 URL /Account/LogOff。

我有一个导航栏,根据用户是登录/注销显示登录/注销:

<div class="nav-collapse collapse">
    <ul class="nav pull-right">
        <li class="dropdown" id="dropdown-login-div">
            @if (!Request.IsAuthenticated)
            {
                <a class="dropdown-toggle" href="#" data-toggle="dropdown">Sign In <strong class="caret"></strong></a>
            }
            else
            {
                @Html.ActionLink("Log Off", "LogOff", "Account")
            }
            <div class="dropdown-menu" id="dropdown-login">
                @Html.Partial("~/Views/Account/_LoginPartial.cshtml", new ViewDataDictionary<LetLord.Models.LoginModel>())
            </div>
        </li>
    </ul>
</div>

在我的帐户控制器中,Internet 模板附带的默认注销操作:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    WebSecurity.Logout();

    return View("Index");
}

有人可以告诉我为什么会发生这种情况 - 在我把笔记本电脑扔到墙上之前。干杯。

4

3 回答 3

41

您使用链接(<a/>标签)注销,当用户单击它时会导致HTTP GET请求,但您的操作仅限于服务POST请求(因为它用[HttpPost]属性装饰)。

您要么需要将链接放入表单并生成 POST 请求,要么从您的操作中删除[HttpPost][ValidateAntiForgeryToken](归功于 GalacticCowboy)。

于 2013-04-15T17:16:22.357 回答
34

由于注销会修改服务器状态,因此我不会删除 [HttpPost] 和 [ValidateAntiForgeryToken] 而是将链接(锚标记)替换为以下内容

@using (Html.BeginForm("Log Out", "Account", FormMethod.Post,
 new { id = "logoutForm" }))
{

    @Html.AntiForgeryToken()
    <a href="javascript:document.getElementById('logoutForm').submit()">Log Out</a>

}
于 2013-04-15T20:54:44.723 回答
0

我在旧版应用程序上遇到了这个问题。我修复它的方法是检测提供的返回 URL 何时为“/Account/LogOff”并采取相应措施。从“AccountController.cs”文件中,“登录”方法:

if (returnUrl == "/Account/LogOff")
    {
        return this.RedirectToLocal(null);
    }
    else
    {
        return this.RedirectToLocal(returnUrl);  
    }    
于 2017-05-18T19:10:28.087 回答