In MVC 4 if you create a new application and select the option for Internet application the template will wire everything for forms authentication and will set you up to use the SimpleMembership provider, which makes it easier to customize user profiles and adds support to easily plugin OAuth. 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 the loginUrl if the user is not authenticated or authorized. Then you just use the AuthorizeAttribute on either your controllers or actions. You can add roles to this attribute if you want to use roles-based authorization or just use it without roles. Here I added an AuthorizeAttribute to the Contact action for the HomeController.
[Authorize(Roles="Admin")]
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
This action is on the default HomeController that is created by the MVC 4 Internet template. The user experience will be that if they click on the Contacts tab on the home page and they are not logged in they will be redirected to the logon page. Once they successfully logon they will be redirected back to the Contacts page. So MVC 4 Internet applications have it all wired up for you and you do not have to explicitly handle the redirects. For more information on customizing the SimpleMembership provider you can read this blog.
RedirectFromLoginPage. Have you considered using anew RedirectResult("/my_front_page");?returnUrl? You can useif (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } return RedirectToRoute("Index");or something similar.RedirectFromLoginPagedrops an authentication cookie and then redirects the person to the location specified inReturnUrlparameter of the query string, otherwise to the defaultUrl.