In MVC 4, if you create a new application and select the option for an Internet application, the template will connect everything for forms authentication and configure you to use the SimpleMembership provider, which will simplify the configuration of user profiles and add support for the OAuth plugin easily. You should have the following entry in your web.config.
<authentication mode="Forms"> <forms loginUrl="~/Account/Login" timeout="2880" /> </authentication>
This tells the application to redirect to loginUrl if the user is not authenticated or authorized. Then you simply use AuthorizeAttribute on your controllers or actions. You can add roles to this attribute if you want to use role-based authorization or just use it without roles. Here I added the AuthorizeAttribute attribute for the Contact action for the HomeController.
[Authorize(Roles="Admin")] public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); }
This action is located in the default HomeController, which is created by the MVC 4 Internet template. The user experience will be that if they click on the "Contacts" tab on the main page and they do not log in, they will be redirected to the login page into the system. After a successful login, they will be redirected back to the "Contacts" page. Thus, MVC 4 Internet applications are all connected for you, and you do not need to explicitly handle redirects. For more information on setting up a SimpleMembership provider, you can read this blog .
source share