Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 能否在web.config中按每个控制器设置用户授权?(不能使用authorized属性)_C#_Asp.net_Authorization_Asp.net Web Api2 - Fatal编程技术网

C# 能否在web.config中按每个控制器设置用户授权?(不能使用authorized属性)

C# 能否在web.config中按每个控制器设置用户授权?(不能使用authorized属性),c#,asp.net,authorization,asp.net-web-api2,C#,Asp.net,Authorization,Asp.net Web Api2,我有一个使用windows auth的Web API 2应用程序。我有多个控制器,在我的web.config中有此控制器用于授权: <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> <authentication mode="Windows" /> <auth

我有一个使用windows auth的Web API 2应用程序。我有多个控制器,在我的web.config中有此控制器用于授权:

<system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Windows" />
    <authorization>
      <allow users="AllowedUsersAndGroups" />
      <deny users="?" />
    </authorization>
    <sessionState mode="Off" />
  </system.web>

这种“一揽子”授权非常有效,但是我有两个特定的控制器需要以不同的方式锁定:我想指定有权访问这两个控制器的不同用户

显而易见的答案是在控制器类上使用
[Authorize()]
属性,但是我不能使用此属性,因为我正在使用每个环境(Dev UAT Prod)XML转换到web.config,并且需要能够更改在每个环境中这些控制器上授权的用户

那么,是否可以在我的web.config文件中为各个控制器指定授权

我对其他解决方案持开放态度,只要它们允许在每个环境中部署应用程序时授权不同的用户(dev uat prod)


谢谢

在我们的环境中,我们使用这种方法:

Active Directory组的名称存储在应用程序设置中。这些名称因环境而异

接下来,我们创建了名为
AuthorizeWritersAttribute
AuthorizeWritersAttribute
的子类型,如下所示:

公共类AuthorizeWritersAttribute:AuthorizeAttribute
{
公共authorizerWritersAttribute()
{
Roles=ConfigurationManager.AppSettings[“解决方案名称:AuthorizedWriters”];
//实际上,我们删除了对ConfigurationManager的依赖,但为了简洁起见,这就足够了。
}
}
最后,我们将此属性应用于控制器:

[authorized writers]
公共类BlogController:控制器
{
....
}

我们使用广告组,但广告帐户也应该起作用。

在我们的环境中,我们创建了一个自定义的
AuthorizeAttribute
,它派生自基本属性。在它中,我们检查每个环境的角色。我会查一个例子。如果你能发布一个例子,那就太好了——我目前正在调查这个问题,所以任何指导都是非常感谢的!好极了——这正是我想要的
我没有使用
ConfigurationManager
,而是改为
Settings.Default.SpecialControllerAuthorizedUsers
以便将该设置与其他设置放在
web.config
中(而不是
部分)