Title- Redirection to login after successfull logout How to automatically redirect to login page after auto logout of the page in mvc5
Asked
Active
Viewed 874 times
2 Answers
1
Since you don't provide any details on your implementation of authentication/authorization, I will assume that you are using MVC5 with individual user accounts and are using OWIN middleware to handle your authentication cookies.
In it's simplest form, put this check if the Request(user) is authenticated in your Home controllers Index method.
public async Task<ActionResult> Index()
{
if (Request.IsAuthenticated)
{
// do something here
return View();
}
return RedirectToAction("Login", "Account");
}
In the Startup class, define your LoginPath as seen here in a standard implementation as assumed above:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
},
SlidingExpiration = true,
// Use this to customize the timeout duration if the default is too short/long
ExpireTimeSpan = TimeSpan.FromDays(14)
});
Lasse Holm
- 245
- 1
- 5
- 14