I am familiarizing myself with asp.net mvc, Identity and trying a simple log off but get The resource cannot be found.
in my html page
<li><a href="@Url.Action("LogOff", "Account")" id="home">Log off</a></li>
and this is in the AccountController
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Index", "Home");
}
and this in my route config file
routes.MapRoute(
null,
"LogOff",
new { controller = "Account", action = "LogOff" }
What could I be missing?
Remove [HttpPost]
from LogOff
method
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Index", "Home");
}
simply putting by removing HttpPost
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Index", "Home");
}
works for me
Use the post method to log off. See the below example.
@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
}
By default when you creating project with some Authentication it creates LogOff
method with HttpPost
attribute.
So You have two ways to fix it
first one is to remove this attribute. And let your Code looks like
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Index", "Home");
}
second one is to change your html to use @Html.BeginForm
with POST method docs here .
By default projects used second type of realization
EDIT While I was typing this answer Rad give an sample how to use BeginForm helper here it is
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.