Windows 使用活动目录组 MVC 进行身份验证

Windows authentication with active directory groups MVC

我为仅在内部使用的 Web 应用实施 Windows 身份验证。这样用户就不需要登录应用程序,而是在登录 Windows.

时已经通过身份验证

我需要根据用户所属的组以及该组对 Active Directory 的权限,授予用户访问应用程序不同部分的权限。

我能够在不同的视图上使用:

[Authorize(Roles = @"Domain\Group")]

我怎样才能在同一个视图上做些该死的事情——根据用户在 Active Directory 中的组权限授予对视图不同部分的访问权限。

<div class="row">
    <div class="col-md-4">
        <h2>User Name: @User.Identity.Name</h2>
        <p>
         Test site with Active Directory
            Only users that belong to Group 1 should see this section
        </p>
        <p></p>
    </div>
    <div class="col-md-4">
        <label>Only users that belong to Group 2 should see this section</label>
    </div>
    <div class="col-md-4">
        <label>Only users that belong to Group 3 should see this section</label>
    </div>

您可以在视图中使用 C#/Razor 条件语句根据用户所属的组来改变内容,如下所示:

<div class="row">

  <div class="col-md-4">
    <h2>User Name: @User.Identity.Name</h2>
    <p>
      Test site with Active Directory
      @if (User.IsInRole("Group1")) {
        Only users that belong to Group 1 should see this section
      }
    </p>
    <p></p>
  </div>
  @if (User.IsInRole("Group2")) {
    <div class="col-md-4">
      <label>Only users that belong to Group 2 should see this section</label>
    </div>
  }

  @if (User.IsInRole("Group3")) {
    <div class="col-md-4">
      <label>Only users that belong to Group 3 should see this section</label>
    </div>
  }