1

MVC3 网站的管理部分被创建为一个区域。然后我将以下代码放入 Web.config

<location path="Admin">
<system.web>
  <authentication mode="Forms">
    <forms loginUrl="~/Admin/Login/Login" timeout="5000" defaultUrl="~/Admin/Login/Redirect" />
  </authentication>
  <authorization>
    <deny users="?"/>
  </authorization>
</system.web>
</location>

但是它会引发错误

解析器错误消息:在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的部分是错误的。此错误可能是由未在 IIS 中配置为应用程序的虚拟目录引起的。

源错误:

Line 44:   <location path="Admin">
Line 45:     <system.web>
Line 46:       <authentication mode="Forms">
Line 47:         <forms loginUrl="~/Admin/Login/Login" timeout="5000" defaultUrl="~/Admin/Login/Redirect" />
Line 48:       </authentication>
4

3 回答 3

0

您不能覆盖<authentication>特定子文件夹的节点。这根本不支持。如果您想为您的区域创建一个不同的登录页面,您可以编写一个自定义[Authorize]属性,然后用它装饰区域中的所有控制器操作。这个想法是仅覆盖HandleUnauthorizedRequest方法并重定向到所需的登录页面。

例如:

public class AdminAuthorizeAttribute : AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        var routeValues = new RouteValueDictionary(new
        {
            controller = "login",
            action = "login",
            area = "admin"
        });
        filterContext.Result = new RedirectToRouteResult(routeValues);
    }
}

就您的 web.config 而言,<authentication>节点必须直接配置在该<system.web>部分下,而不是在一个<location>部分内。

于 2013-03-13T07:27:16.307 回答
0

我试图解释

如果您的应用程序包含多个区域,则当应用程序运行时,所有区域都会被注册。以便登录用户可以访问每个区域。

但是,如果您只想允许访问特定区域,则需要覆盖默认区域注册过程。

在那个过程中,我们去掉了,所有的路由都属于每个Area

while(RouteTable.Routes.Count > 0) RouteTable.Routes.RemoveAt(0);

之后,我们只允许那些我们希望允许登录用户的区域,因为我们使用了

MvcApplication app = (MvcApplication)HttpContext.ApplicationInstance;
RouteCollection existingcoll = outeCollection)app.Application["ExistingRoutecolling"];

foreach (Route _route in existingcoll)
 {
    // allow only those routes , which belongs to the area which you want allow to access the login user
    if (_route.Url == "Admin/{controller}/{action}/{id}")
        RouteTable.Routes.Add((RouteBase)_route);       
    // re-register routes again
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    // now redirect with expected action
    return RedirectToAction("controller", "action", new { area = "Admin" });
}
于 2016-06-26T02:57:16.970 回答
-1
public class MvcApplication : System.Web.HttpApplication
 {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            RouteCollection existingcoll = new RouteCollection();

            foreach (Route _route in RouteTable.Routes)
                existingcoll.Add((RouteBase)_route);
            //keep all default registerd routes in Asp Application object  
            Application["ExistingRoutecolling"] = existingcoll;
        }
}
//after login when user called first action to render dashboard, you can add logic there
 public ActionResult ModuleDashboard
 {
     //get default registerd routes from Asp Application object which we stored in  Application_Start() method 
    MvcApplication app = (MvcApplication)HttpContext.ApplicationInstance;
    RouteCollection existingcoll = (RouteCollection)app.Application["ExistingRoutecolling"];

    // remove all register routes, by default those are registered by application object
    while (RouteTable.Routes.Count > 0)
        RouteTable.Routes.RemoveAt(0);

    //navigate each route from collection and add in actual application route collection object         
     foreach (Route _route in existingcoll)
     {
        // allow only those routes , which belongs to the area which you want allow to access the login user
        if (_route.Url == "Admin/{controller}/{action}/{id}")
            RouteTable.Routes.Add((RouteBase)_route);       
        // re-register routes again
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        // now redirect with expected action
        return RedirectToAction("controller", "action", new { area = "Admin" });
    }

 }
于 2016-06-23T13:02:26.573 回答