Use the [Authorize] attribute on your controller.
[Authorize] public class YourController: Controller { . . . [AllowAnonymous] public ActionResult Register() { } [AllowAnonymous] public ActionResult LogIn() { } . . . }
In addition, you must add your login page to web.config -
<system.web> <authentication mode="Forms"> <forms loginUrl="~/Login" timeout="2880" /> </authentication> </system.web>
You have another better option for registering AuthorizeAttribute as a global filter in the global.asax file.
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { .... filters.Add(new System.Web.Mvc.AuthorizeAttribute()); }
Therefore, you should only apply [AllowAnonymous] to the actions that you want to be visited by anonymous users.
source share