ActiveDirectory DirectorySearcher: check if the user is a member of a group

I want to know if this user is a member of a group or not. Now I don’t know much about ActiveDirecory or Exchange servers, but in Outlook I can see that the user can be a “memberOf” group (and I can request these groups using DirectorySearcher.PropertiesToLoad.Add("memberof");), but there are other groups in which users are not active members. but which contain users. If you send mail to these groups (or aliases), you will reach all users included in it.

Basically, given a username (e.g. DOMAIN\JDoe), how do I check if it is contained in a group FUNNY_USERSin C #?

+4
source share
3 answers

Use the System.DirectoryServices.AccountManagement namespace added in .Net 3.5, if available. Here is an example of a group check:

using(var pc = new PrincipalContext(ContextType.Domain))
using(var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, "DOMAIN\JDoe"))
using(var group = GroupPrincipal.FindByIdentity(pc, "FUNNY_USERS"))
{
    return user.IsMemberOf(group);
 }
+12
source

, Outlook, , . Active Directory . , /.

#, ldap-

+1
source

Source: https://habr.com/ru/post/1736470/


All Articles