GroupPrincipal.FindByIdentity(principleContextGroup, "security group name")
how can i integrate this in the code below ?
As far as I think,it's another way to filter if the user is the part of the security group using GroupPrincipal.FindByIdentity.
You could do just like:
using (PrincipalContext principleContext = new PrincipalContext(ContextType.Domain, m_activeDirectoryServer, "OU=" + courseType + m_domainControllerString, userName: m_activeDirectoryUser, password: m_activeDirectoryPassword)) { using (UserPrincipal userPrincipal = new UserPrincipal(principleContext) { Name = "*", EmailAddress = "*" }) { using(var grp = GroupPrincipal.FindByIdentity(principleContext , yourGroup)) { if (grp.Members.Contains(userPrincipal)) { return true; } } } }
Best regards,
Yijing Sun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Thans for your answer, i need a list of all users in an OU and that are in a Security group in that OU,
How do i get this part in your code ? The foreach part to build the usersList
Member
41 Points
129 Posts
see if a user is in a security group
Jan 14, 2021 02:08 PM|Bradly|LINK
I have the following code , and i would like to filter if the user is in a security group.
i found the following code :
GroupPrincipal.FindByIdentity(principleContextGroup, "security group name")
how can i integrate this in the code below ?
Thanks
Contributor
3410 Points
1294 Posts
Re: see if a user is in a security group
Jan 15, 2021 02:54 AM|yij sun|LINK
Hi Bradly,
As far as I think,it's another way to filter if the user is the part of the security group using GroupPrincipal.FindByIdentity.
You could do just like:
Best regards,
Yijing Sun
Member
41 Points
129 Posts
Re: see if a user is in a security group
Jan 15, 2021 11:27 AM|Bradly|LINK
Hi Yij sun,
Thans for your answer, i need a list of all users in an OU and that are in a Security group in that OU,
How do i get this part in your code ? The foreach part to build the usersList
{ foreach (UserPrincipal p in results) { allUsers.Add(new AdUser { DisplayName = p.DisplayName, EmailAddress = p.EmailAddress, ExpirationDateTime = p.AccountExpirationDate, Enabled = p.Enabled, OrganizationalUnit = courseType }); } }
Thank you,
Best regards, Bradly
Member
41 Points
129 Posts
Re: see if a user is in a security group
Jan 17, 2021 02:16 PM|Bradly|LINK
Figured it out see below