检查当前用户是否是活动目录组的成员

Dee*_*pak 4 c# active-directory active-directory-group activedirectorymembership

我需要检查当前用户是否是活动目录组的成员.我开始将当前用户设置如下.现在我想知道如何检查此CurrentUser是否在活动目录组"CustomGroup"中

string CurrentUser = WindowsIdentity.GetCurrent().Name;
Run Code Online (Sandbox Code Playgroud)

JPB*_*anc 12

您可以使用.NET 3.5 System.DirectoryServices.AccountManagement类.有关详细信息,请参阅MSDN文章管理.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
    }
}
Run Code Online (Sandbox Code Playgroud)