MVC4区和窗体身份验证 [英] MVC4 areas and forms authentication
问题描述
我有一个MVC4应用程序中设置了多个区域。每个区域必须有自己的登录页面。比方说,比如我有以下几个方面:
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:
主要
管理员
如何设置它,以便主区域都有不同的登录页面的管理区域?我想web.config中是不是走在这条路上。
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.
目前我有我的根Web.config文件中的以下内容:
Currently I have the following in my root web.config file:
<authentication mode="Forms">
<forms loginUrl="~/Admin/Login" timeout="2880" protection="Encryption" />
</authentication>
不过,我努力工作,如何与这个领域的适应MVC4。
However, I'm struggling to work out how to adapt this to MVC4 with areas.
请帮忙。
推荐答案
非常感谢UfukHacıoğulları为把我右边的香味。我最终的解决办法是这样的:
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);
}
}
这篇关于MVC4区和窗体身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!