Click here to Skip to main content
15,868,127 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using mvc 5 and i want to check the session is null or not if null then redirect to login page .
How to check and redirect to login from master page (_Layout.chtml).

What I have tried:

@{
   Everest.Common.UserCredentialTO userCredential = ViewBag.UserCredential;
}
@if (userCredential == null || userCredential.UserId == -1)
{
   --redirect to login page.
}
Posted
Updated 27-Jan-17 17:57pm
v2
Comments
Member 7870345 30-Jan-17 2:30am    
I think that to redirect in the _layout.cshtml is imposible. But in your controllers methods you can use the attribute [Authorize] that cause that is the user is not autorized to execute that method he will redirect to the login page.

If you google "authorize attribute mvc example" you can see examples of how to use.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900