用户授权可以设置在每个控制器的基础上在web.config?(不能使用AuthorizeAttribute)

本文关键字:不能 config AuthorizeAttribute web 授权 设置 用户 控制器 基础上 | 更新日期: 2023-09-27 18:15:25

我有一个使用windows认证的Web API 2应用程序。我有多个控制器,在我的web中。配置授权:

<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。配置和需要能够在每个环境中更改在这些控制器上授权的用户。

那么,是否有可能在我的web中为单个控制器指定授权?配置文件?

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

谢谢!

用户授权可以设置在每个控制器的基础上在web.config?(不能使用AuthorizeAttribute)

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

Active Directory组的名称存储在app-settings中。这些名称在每个环境中是不同的。

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

public class AuthorizeWritersAttribute : AuthorizeAttribute 
{
    public AuthorizeWritersAttribute()
    {
        Roles = ConfigurationManager.AppSettings["SolutionName:AuthorizedWriters"];
        // Actually we removed the dependency on ConfigurationManager but for brevity this suffices.
    }
}

最后我们把这个属性应用到我们的控制器上:

[AuthorizeWriters]
public class BlogController : Controller
{
    ....
}

我们使用ad -group,但是ad -account也可以。