我一直在讨论这个问题很久了,在任何地方都找不到答案,但是我知道许多公司都在使用我想做的事情,所以我决定将其放在这里。
我想做的是:
这是一个例子:
我知道我的用户在我的数据库用户/组表中有一个条目:我知道他在AD组中,名为“ MyAppGroup \ MyDomain”。 从数据库的组列表中查找用户的最简单方法是什么?
答案 0 :(得分:0)
如评论中所述,您要查找的数据已存储在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;
}