Reputation: 345
I decided to use the asp.net identity in my site for the first time. I am trying to create a simple logout link from my view. When I click the link I am get an error "Server Error in '/' Application. Requested URL: /Account/Logout". At this point it seems to me that the code is correct but obviously something is wrong. Any help would be appreciated.
Controller:
// POST: /Account/Logout
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Logout()
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie);
Session.Abandon();
return RedirectToAction("Login", "Account");
}
Index View:
<div id="sidebar-nav" class="sidebar">
<div class="sidebar-scroll">
<nav>
<ul class="nav">
<li><a href="@Url.Action("Index", "Home")" class="active"><i class="lnr lnr-home"></i> <span>Dashboard</span></a></li>
<li>
<a href="#subPages" data-toggle="collapse" class="collapsed"><i class="lnr lnr-file-empty"></i> <span>Reports</span> <i class="icon-submenu lnr lnr-chevron-left"></i></a>
<div id="subPages" class="collapse ">
<ul class="nav">
<li><a href="@Url.Action("Index", "Home")" class="">Fulfillment</a></li>
<li><a href="@Url.Action("Index", "Home")" class="">Report</a></li>
<li><a href="@Url.Action("Index", "Home")" class="">Report</a></li>
</ul>
</div>
</li>
<li><a href="@Url.Action("Logout", "Account")" class=""><i class="lnr lnr-dice"></i> <span>Logout</span></a></li>
</ul>
</nav>
</div>
</div>
When the logout link is clicked in the index view it returns the error described above.
The expected results are for the link to call the Logout method in the controller and redirect to the Login page.
Upvotes: 3
Views: 1715
Reputation: 8597
Since you're accessing the link by clicking on it, you should be attributing your endpoint with [HttpGet]
rather than [HttpPost]
as that's the type of the request you're making.
You also should add the anti-forgery token to your site since you're validating it by attributing the method with [ValidateAntiForgeryToken]
(Applicable to POST requests)
If you do not want to do so (or when you make GET requests), remove the annotation otherwise add the following to your form to generate one:
@Html.AntiForgeryToken()
This in turn will render input similar to:
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8NrAkS ... s2-m9Yw">
Read more about AntiForgery tokens here.
Upvotes: 3