How can we set authorization for a whole area in ASP.NET MVC?

前端 未结 7 1363

I\'ve an Admin area and I want only Admins to enter the area. I considered adding the Authorized attribute to every controller in the Admin area. Isn\'t there an elegant sol

7条回答
  •  清歌不尽
    2020-12-08 04:28

    Web.config-based security should almost never be used in an MVC application. The reason for this is that multiple URLs can potentially hit a controller, and putting these checks in Web.config invariably misses something. Remember - controllers are not associated with areas, routes are associated with areas. The MVC controller factory will happily serve controllers from the Areas/ folder for non-area requests if there's no conflict.

    For example, using the default project structure, adding an Admin area with an AdminDefaultController, you can hit this controller via /Admin/AdminDefault/Index and /AdminDefault/Index.

    The only supported solution is to put your attribute on a controller base class and to ensure that each controller within the area subclasses that base class.

提交回复
热议问题