可以在 web.config 中基于每个控制器设置用户权限吗? (不能使用 AuthorizeAttribute)

Can user authorization be set on a per-controller basis in web.config? (cannot use AuthorizeAttribute)

我有一个使用 windows 身份验证的 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>

这个 "blanket" 授权效果很好,但是我有 2 个特定的控制器需要以不同方式锁定:我想指定不同的用户被授权访问这 2 个控制器.

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

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

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

谢谢!

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

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

接下来我们创建了一个名为 AuthorizeWritersAttributeAuthorizeAttribute 子类型,如下所示:

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 组,但 ​​AD 帐户应该也能正常工作。