简体   繁体   中英

ASP.NET MVC Role Authorization based on Site/URL

I'm creating a single web application that I will be deploying to different sites (eg, red.domain.com, blue.domain.com, etc). I want different roles to have access to the different sites (eg, the "red" role will have access to red.domain.com).

Currently I am using controller-wide authentication (eg, [Authorize(Roles = "red")]). When I deploy my application to a site, I have to change the role on each controller (eg from "red" to "blue"), which seems ridiculous.

Anyone have any ideas on how I can streamline this? Maybe a setting I can create on the IIS site itself that automatically applies the correct role?

Thanks much.

Could be good place for dependency injection. A library like StructureMap allows you to define type mappings in web.config (I'm assuming you have one web.config for each site)

So, if you have RedController and BlueController , that both implement a common interface IColourfulController it would look something like this:

<!-- red web.config-->
 <DefaultInstance
      PluginType="IColourfulController, [assembly name]"
      PluggedType="RedController,  [assembly name]" />

<!-- blue web.config-->
 <DefaultInstance
      PluginType="IColourfulController, [assembly name]"
      PluggedType="BlueController,  [assembly name]" />

// when you need the controller
IColourfulController controller = 
    ObjectFactory.GetInstance<IColourfulController>();

Both sites can use the same code for getting the controller, but the web.config says whether it will be blue or red.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM