MVC4 areas and forms authentication
asp.net-mvc, asp.net-mvc-4, forms-authentication
Solution
Many thanks to Ufuk Hacıoğulları for putting me on the right scent. My final solution was this:
public class AreaAuthorizeAttribute : AuthorizeAttribute
{
private readonly string area;
public AreaAuthorizeAttribute(string area)
{
this.area = area;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
string loginUrl = "";
if (area == "Admin")
{
loginUrl = "~/Admin/Login";
}
else if (area == "Members")
{
loginUrl = "~/Members/Login";
}
filterContext.Result = new RedirectResult(loginUrl + "?returnUrl=" + filterContext.HttpContext.Request.Url.PathAndQuery);
}
}
Problem
I have an MVC4 application set up with multiple areas. Each area must have its own login page. Let's say for example I have the following areas: Main Admin How can I set it so that the "Main" area has a different login page to the "Admin" area? I'm thinking web.config is not the way to go on this. Currently I have the following in my root web.config file: ``` <authentication mode="Forms"> <forms loginUrl="~/Admin/Login" timeout="2880" protection="Encryption" /> </authentication> ``` However, I'm struggling to work out how to adapt this to MVC4 with areas. Please help.