[英]Using AD group names stored in sql database for authorizing users in C# app
我一直在討論這個問題很久了,在任何地方都找不到答案,但是我知道許多公司都在使用我想做的事情,所以我決定將其放在此處。
我想做的是:
所以這是一個例子:
我知道我的用戶在我的數據庫用戶/組表中有一個條目:我知道他在AD組中,名為“ MyAppGroup \\ MyDomain”。 從數據庫的組列表中找出用戶所在的最簡單方法是什么?
如注釋中所述,您要查找的數據已經存儲在Active Directory中;例如, 您根本不需要將其添加到數據庫中。
您可以使用System.DirectoryServices.AccountManagement API查詢AD(包括組成員身份和大量其他數據)。
這是一個有關如何檢索用戶所屬的組的小示例:
using System.DirectoryServices.AccountManagement;
// ...
public List<string> GetGroupsForUser(string domain, string ou, string samAccountName)
{
var groups = new List<string>();
using (var principalContext = new PrincipalContext(ContextType.Domain, domain, ou))
using (var userPrinicpal = UserPrincipal.FindByIdentity(principalContext,
IdentityType.SamAccountName, samAccountName))
{
if (userPrinicpal == null)
return null;
foreach (var securityGroup in userPrinicpal.GetAuthorizationGroups())
groups.Add(securityGroup.DisplayName);
}
return groups;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.