I'm making a new (empty template) ASP.NET MVC 5 application and I cannot logoff of this app. My logoff Action:
public ActionResult LogOff()
{
if (User.Identity.IsAuthenticated)
{
//break here
}
try
{
AuthenticationManager.SignOut();
if (User.Identity.IsAuthenticated || Request.IsAuthenticated)
{
//break here;
}
}
return RedirectToAction("Login", "Account");
}
Startup class:
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
}
Application Context:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", false)
{
}
}
Connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=.;Database=DataTest;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
The action LogOff() executes without problems and redirects me to the 'Login' action but I am still logged in. What is wrong with it?
About ASP .Net MVC Logout not working:-
I had a problem where app hosted on IIS in production modes was not working right with chrome
though it was worked right while - using Visual Studio Dev hosting in all browsers - in production mode over IE
I had problems in Startup.Auth.CS. Make sure duplicate configurations are not there for following things
This worked for me: create a route in your RouteConfig.cs like
And you can maintain the default logoff code in AccountController.cs or add the additions(like
session.abandon();
etc) others have suggested But just as below should workTry this:
^^Set the "LogoutPath" in Startup.Auth.cs to whatever route you desire
This seems to work well for me.
In this case you could also do the following: Remove the [HttpPost] from your LogOff action and put the [HttpGet] instead. You only need to pass the AntiForgeryToken. But the question will be if this is a very secure way. more information available here: Using MVC3's AntiForgeryToken in HTTP GET to avoid Javascript CSRF vulnerability