看来我使用了错误的设置。如果要使用标准 AD,请选择 Windows 身份验证。
“工作或学校帐户”似乎仅适用于 ADFS。
一旦您的应用程序被初始化,请转到 web.config。
你会看到这样的东西
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
我们需要将 AD 组功能添加到我们的 web.config。完成此操作后,您的 system.web 部分应如下所示:
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
<!--NEW-->
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider">
<providers>
<clear />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
您现在可以使用以下内容装饰您的控制器类/操作方法:
[Authorize(Roles = @"YourDomain\Some AD Group Name")]
或常用代码:
User.IsInRole(@"YourDomain\Some AD Group Name"))