我一直在嘗試執行註銷操作(使用cookie的表單身份驗證),但無法調用POST註銷操作方法。ASP.net MVC無法調用POST註銷方法
這裏是我的局部視圖:
<nav class="navbar navbar-inverse" role="navigation">
<div class="container">
<div class="nav navbar-nav navbar-right">
@using (Html.BeginForm("Logoff", "Account", FormMethod.Post, new { id = "logoutForm" }))
{
@Html.AntiForgeryToken()
<a class="navbar-brand" href="javascript:document.getElementById('logoutForm').submit()" title="Logout">
<img alt="Home" src="@Url.Content(" ~/Images/logout.png ")"/>
</a>
}
</div>
</div>
</nav>
我打電話下面主頁這部分查看標題:
<header role="banner">
@Html.Partial("_HomeHeader")
</header>
<div class="container">
.......
</div>
在我的帳戶控制器,我定義了以下操作方法:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Logoff()
{
FormsAuthentication.SignOut();
Session.Clear();
return Redirect(FormsAuthentication.LoginUrl);
}
因此,單擊主頁上的註銷按鈕時,應該轉到「註銷」操作方法並清除cookie,然後重定向到登錄頁面。 但它不起作用。 而是直接進入登錄頁面而不訪問註銷操作方法。我得到這個網址: http://localhost/Account/Login?ReturnUrl=%2FAccount%2FLogoff
任何人都可以請提供他們關於如何實現此功能的輸入?
當您嘗試訪問的頁面需要身份驗證並且您未經身份驗證或權限不足時,通常會獲得'/ Account/Login?ReturnUrl = blah'。 –
在這種情況下,'Logoff'操作方法需要身份驗證來訪問它,並且可能'FormsAuthentication.SignOut()'在註銷過程中移除授權cookie - 嘗試使用'AllowAnonymousAttribute'標記'Logoff'操作方法。 –
添加[AllowAnonymous]屬性爲我工作。感謝Tetsuya。 –