[英]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.