Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC truly log off with Forms Authentication

Tags:

I have a logoff action on a controller as so:

    public ActionResult Logoff()     {         var x = Request.IsAuthenticated;         var y = User.Identity.IsAuthenticated;          FormsAuthentication.SignOut();         Session.Abandon();          var a = Request.IsAuthenticated;         var b = User.Identity.IsAuthenticated;          return View();     } 

However, x, y, a, and b, are all true. So when my view renders, it still behaves as if the user is logged in. Can someone please provide a solution and/or explanation?

like image 426
CodeGrue Avatar asked May 11 '10 11:05

CodeGrue


People also ask

What is FormsAuthentication SignOut ()?

Removes the forms-authentication ticket from the browser. public: static void SignOut();

How do I remove authentication from a cookie form?

You first need to Clear the Authentication Cookie and Session Cookie by passing back empty cookies in the Response to the Logout. public ActionResult LogOff() { FormsAuthentication. SignOut(); Session. Clear(); // This may not be needed -- but can't hurt Session.


1 Answers

FormsAuthentication.SignOut() removes the authentication cookie, so you need to redirect after it instead of returning a view so that the client is notified:

public ActionResult Logoff() {     FormsAuthentication.SignOut();     return RedirectToAction("Index"); } 

Now in the Index action the user will no longer be authenticated.

like image 99
Darin Dimitrov Avatar answered Sep 27 '22 22:09

Darin Dimitrov