I am developing a web application using ASP.Net MVC 4, the application has one login page and 5 other pages which will be accessible by logged in users only. So how do i globally check the existence of session in all the pages without writing the following code in all the other pages(Actions) : -
if (Session["login"] != null)
{
return View();
}
else
{
return RedirectToAction("LoginPage");
}
Is there any other way to check session (authorization) globally and redirect to login page(Action)? Is this possible via Layout file? Like i'll create a common header layout to all the pages which will be accessed by logged in users so in that header layout is it possible to check for the session(authorization) and redirect to the login page (if not logged in) ?
Authorize
attribute. asp.net/web-api/overview/security/… – ramiramiluAuthorize
attribute mentioned by @ramiramilu does. That code project linked article is old, and the way to go today is to use asp.net/identity – Brendan Green