A fellow developer and I are porting a WebForms project over into MVC3, and we’re new to this. We’ve got our WebForms master page ported to an MVC3 layout page, but the one thing that throws us is the Session for a logged in user.
In the WebForms master page, we check the Session to make sure Session(“LoggedInUser”) has a value, and if not, redirect to login page.
We’re not sure how to replicate this in MVC3, and my googling has come up short. It seems kinda evil to place that sort of logic in the layout page, but maybe I’m nuts. Surely, there’s a better way to do this than putting a check like that in every single action method… If someone could point us in the right direction, we’ll gladly dive in and research.
Much thanks.
All you have to do is add [Authorize] tag on all controllers. It’ll automatically redirect you to login page specified in web.config if you are not logged in.
Even better you could create a BaseController from which each other controller will inherit.
BaseController
Other controllers
Each action in HomeController will redirect based on your FromsAuthentication settings.