【问题标题】:Check whether current user is a member of an active directory group检查当前用户是否是活动目录组的成员
【发布时间】:2012-02-05 20:21:47
【问题描述】:

我需要检查当前用户是否是活动目录组的成员。我开始获取当前用户,如下所示。现在我想知道如何检查这个 CurrentUser 是否在活动目录组“CustomGroup”中

string CurrentUser = WindowsIdentity.GetCurrent().Name;

【问题讨论】:

标签: c# active-directory activedirectorymembership active-directory-group


【解决方案1】:

您可以使用 .NET 3.5 System.DirectoryServices.AccountManagement 类。有关详细信息,请参阅 MSDN 文章 Managing Directory Security Principals in the .NET Framework 3.5。你可以使用类似的东西:

string CurrentUser = WindowsIdentity.GetCurrent().Name;

PrincipalContext context = new PrincipalContext(ContextType.Domain, "Domain");
UserPrincipal upUser = UserPrincipal.FindByIdentity(context, CurrentUser);
if(upUser != null)
{
    if (upUser.IsMemberOf(context, IdentityType.SamAccountName, "CustomGroup")) 
    {
        // The user belongs to the group
    }
}

【讨论】:

  • 应该 upUser .IsInRole("CustomGroup")) 是 upUser.IsMemberOf("CustomGroup")) 吗?
  • 用户可以伪造他的 ID 和域吗?
【解决方案2】:

在 .NET 3.5 或 4 中试试这个:

PrincipalContext infPC = new PrincipalContext(ContextType.Domain, "domain", "login", "password");
UserPrincipal infUP = new UserPrincipal(infPC);
PrincipalSearcher infPS = new PrincipalSearcher();
UserPrincipal foundUP;
GroupPrincipal infGP = new GroupPrincipal(infPC);
GroupPrincipal foundGP;
string CurrentUser = WindowsIdentity.GetCurrent().Name;

infUP.SamAccountName = CurrentUser;
infPS.QueryFilter = infUP;
foundUP = infPS.FindOne();
infGP.Name = "CustomGroup";
infPS.QueryFilter = infGP;
foundGP = infPS.FindOne();
bool ismember = foundUP.IsMemberOf(foundGP);

【讨论】:

  • 我们知道要在哪个组中搜索当前用户。所以会有任何直接的过程来做到这一点。比如从 AD 中获取我们想要搜索的组并遍历该组中当前用户的用户?
  • 这是你写的:“我需要检查当前用户是否是活动目录组的成员。”。所以你需要来自特定组的所有用户?
  • 这个你需要用户名和密码,有没有办法在不知道这个的情况下检查当前的 WindowsIdentity 是否是组的成员?并且不遍历身份的整个组列表?
  • 您必须是您要查询的域的成员,并具有特定的用户权限。检查这里link或这里link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多