简体   繁体   English

基于站点/ URL的ASP.NET MVC角色授权

[英]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). 我正在创建一个Web应用程序,该应用程序将部署到不同的站点(例如red.domain.com,blue.domain.com等)。 I want different roles to have access to the different sites (eg, the "red" role will have access to red.domain.com). 我希望不同的角色可以访问不同的站点(例如,“红色”角色将可以访问red.domain.com)。

Currently I am using controller-wide authentication (eg, [Authorize(Roles = "red")]). 当前,我正在使用控制器范围的身份验证(例如[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? 也许我可以在IIS站点本身上创建的设置会自动应用正确的角色?

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) 诸如StructureMap之类的库允许您在web.config中定义类型映射(我假设每个站点都有一个web.config)。

So, if you have RedController and BlueController , that both implement a common interface IColourfulController it would look something like this: 所以,如果你有RedControllerBlueController ,双方实现共同的接口IColourfulController它会是这个样子:

<!-- 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. 两个站点都可以使用相同的代码来获取控制器,但是web.config会说明它是蓝色还是红色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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