Having different login pages for different ASP.NET MVC 3 areas

后端 未结 2 656
孤城傲影
孤城傲影 2020-12-29 05:47

Can I have different login URL for different ASP .NET MVC3 areas?

e.g. I would like to have different login page for Administrator and Data entry operators.

2条回答
  •  -上瘾入骨i
    2020-12-29 06:27

    I'm not aware of .NET handling this for you but you could create a custom AuthorizationAttribute

    public class CustomAuthorization : AuthorizeAttribute {
    
      public string Url { get; set; }
    
      public override void OnAuthorization(AuthorizationContext filterContext) {
    
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated) {
          filterContext.HttpContext.Response.Redirect(Url);
        }
        base.OnAuthorization(filterContext);
    
      }
    
    }
    

    An add that to your controllers/actions

    [CustomAuthorization(Url="/Area/Login")]
    public class HomeController {
      //...
    }
    

提交回复
热议问题