Redirect user to login page if session null in MVC 5 Razor syntax

Redirect user to login page if session null in MVC 5 Razor syntax

For Example using MVC 5 Razor:

 @{
     //get user session.
     var session = UserManager.Session();
     if (session == null)
     {
        Response.Redirect("~/Account/LogOff");
     }
 }
                                          OR

We can also use the custom action filter to achieve this functionality. i.e.

For Example :

// Override onAuthentication filter to achieve this functionality.
//and also get user session to check is null value.
protected override void OnAuthentication(AuthenticationContext filterContext)
{
      session = UserManager.Session();
      if ( session == null)
      {
        RedirectToControllers(Helper.Control.ACCOUNT, Helper.Action.ACCOUNT_LOGIN);
      }

      base.OnAuthentication(filterContext);
}



ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book