Find groups that a user belongs to

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to write some code that will find all the groups that a user is a member of

Does anyone know how to do this in VB.NET

Thanks in advanc

Matt
 
You can query Active Directory. There are plenty of samples on the web, but
only works if you have AD installed.
Or, you can use p/invoke to call the NetUser* functions (primarily
NetUserGetGroups). However, the API functions involved aren't very
..NET-friendly in terms of how they allocate buffers and declare parameter
types.
Alternately, you create a WindowsPrincipal object (there are several samples
in the MSDN help). The WindowsPrincipal object will tell you if a user
belongs to a particular group with the IsInRole method of that object.
Internally, this object keeps a list of group names, but you can only get at
this list by using Reflection (you'll have to bind Instance + NonPublic to
get at it - the list is a private field). Actually, there are two lists (one
array and one hash table if i remember correctly). One list is used if the
user is a member of less than a certain number of groups (20-something i
believe), and the other if the user is a member of more groups.

-Rob Teixeira [MVP]
 
you can use WMI...

something like

Imports System.Management

....

Dim myDomain As String = "DomainName"
Dim theUser As String = "LookForMe"
Dim query As String = _
"SELECT * FROM Win32_GroupUser " + _
"WHERE PartComponent = ""Win32_UserAccount.Domain='" + myDomain +
"',Name='" + theUser + "'"""
Dim selectQuery As New SelectQuery(query)
Dim searcher As New ManagementObjectSearcher(sq)
Dim obj As ManagementObject
For Each obj In searcher.Get()
Debug.WriteLine(obj.GetPropertyValue("GroupComponent"))
Next obj


hope this helps

dominique
 
Back
Top