使用AD身份验证创建MVC应用程序(非ADFS)

时间:2017-03-15 17:02:35

标签: asp.net-mvc authentication asp.net-mvc-5 active-directory

我正在Visual Studio 2017中创建一个新的MVC Intranet应用程序。

我想使用Active Directory来验证(和授权)各种控制器,但不使用Active Directory Federation Sever(因为我们不使用它)。

以下是我采取的步骤:

  1. 新项目 - > ASP.NET Web应用程序(.NET Framework)
  2. 选择MVC
  3. 点击更改身份验证
  4. 选择“工作或学校帐户”
  5. 选择“内部部署”
  6. 现在,On-Premises Authority需要一个元数据文档URL。我知道这是ADFS的外部网址...我们没有。

    如何设置我的应用程序以便它使用我们的AD?我还需要在AD中使用群组(例如,允许 WebAdmin 组中的用户访问管理员控制器等)。

    提前致谢

1 个答案:

答案 0 :(得分:0)

我似乎使用了错误的设置。如果要使用标准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>

现在,您可以使用以下命令修饰控制器类/操作方法:

[授权(Roles = @“YourDomain \ Some AD Group Name”)]

或通常的代码:

User.IsInRole(@“YourDomain \ Some AD Group Name”))