簡體   English   中英

根據用戶所在的組顯示 HTML 代碼

[英]Display HTML code depending on which group a user is in

我想根據用戶所在的 AD 組有選擇地在頁面上顯示一段文本。該頁面是一個簡單的 ATM 鏈接列表,不需要控制器。 我有下面的代碼,當我在本地開發(我登錄到 AD)時,它可以完美運行 - 一旦我將應用程序發布到 IIS 服務器,我就會收到 404 錯誤 - 我已經能夠找到導致的確切行錯誤 -> 在 ActiveDirectory.IsInGroup() 行 group.Translate 是罪魁禍首。

我已經檢查了 IIS 服務器上的事件查看器(以及 IIS 的日志),但根本沒有記錄任何內容?

這是 index.html:

@page
@using System.Security.Principal
@{
    ViewData["Title"] = "Landing Page";
}

@{    
   var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
   bool itUser = ActiveDirectory.IsInGroup(principal, "IT Department");
}

<h4>Header</h4>

@if (itUser || adminUser)
    {
       <div class="card mb-4 shadow-sm">
          <div class="card-header">
             <h4 class="my-0 font-weight-normal">IT</h4>
          </div>
          <div class="card-body">
             <a target="_blank" href="http://www.test.com/Configuration/Index" class="btn btn-secondary my-2">Application Config</a><br />
          </div>
       </div>
    }

這是 C# 代碼:

public static class ActiveDirectory

       public static bool IsInGroup(ClaimsPrincipal checkUser, string checkGroupName)
    {
        var identity = (WindowsIdentity)checkUser.Identity;
        if (identity?.Groups == null) return false;

        foreach (var group in identity.Groups)
        {
            var groupName = group.Translate(typeof(NTAccount)).ToString(); // this gives me a 404 error
            if (groupName.ToLower().Contains(checkGroupName.ToLower())) return true;
        }

        return false;
    }
}

不使用控制器是不好的做法(並且違背了使用 MVC 框架(模型視圖控制器)的目的),尤其是在建立可審計性和身份驗證/授權檢查方面。

最佳實踐是讓您的控制器上的默認 ActionResult(在您的 RouteConfig 文件中設置)調用您定義的函數以獲取最終用戶所屬組的列表

例子:

在每個控制器 ActionResult 中傳遞以下內容:

// what will be needed:    
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;



var userSignOn = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name;

List<string> activedirectoryGroupList = new List<string>();

activedirectoryGroupList = GetGroupsFromSignOn(userSignOn);

被調用的函數:

internal static List<string> GetGroupsFromSignOn(string signOn)
{
    string searchText = "(&(objectCategory=person)(objectClass=user)(sAMAccountName={0}))";
    searchText = searchText.Replace("{0}", signOn);

    Domain domain = Domain.GetCurrentDomain();
    DirectoryEntry userEntry = domain.GetDirectoryEntry();

    DirectorySearcher searcher = new DirectorySearcher(userEntry, searchText);
    SearchResult result = searcher.FindOne();

    DirectoryEntry currentUserEntry = result.GetDirectoryEntry();

    List<string> activedirectoryGroupList = new List<string>();

    if (currentUserEntry != null)
    {           
        int propertyCount = currentUserEntry.Properties["memberOf"].Count;

        string activedirectoryGroup;

        for (int i = 0; i < propertyCount; i++)
        {
            activedirectoryGroup = Convert.ToString(currentUserEntry.Properties["memberOf"][i]);

            int startIndex = activedirectoryGroup.IndexOf('=');
            startIndex += 1;

            int endIndex = activedirectoryGroup.IndexOf(',');

            int stringLength = endIndex - startIndex;

            activedirectoryGroup = activedirectoryGroup.Substring(startIndex, stringLength);

            activedirectoryGroupList.Add(activedirectoryGroup);
        }

        return activedirectoryGroupList;
    }
    else
    {
        return null;
    }
}

從那里,您將獲得您的用戶所屬的 AD 組的列表,並且您可以將這些組與您可以在 Web 配置文件中設置的授權組名稱交叉引用,或者在您可以調用的 SQL 數據庫。

至於確定將顯示什么,您可能應該設置一個主視圖,根據您的用戶對該信息的授權狀態在該頁面上呈現部分視圖。

編輯:

關於使用 asp.net MVC 進行身份驗證的挑戰的有用線程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM