
Below is a code snippet that allows you to determine what groups a Windows user is part of.
[C#]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
namespace KeithRull.ActiveDirectory.IdentifyingUserGroups
{
class Program
{
static void Main(string[] args)
{
//get the WindowsIdentity of the current user
WindowsIdentity currentWindowsIdentity = WindowsIdentity.GetCurrent();
//retrieve the groups that the user belongs to
IdentityReferenceCollection currentWindowsIdentityGroups = currentWindowsIdentity.Groups;
//iterate thru each group
foreach (IdentityReference identity in currentWindowsIdentityGroups)
{
//translate the identity into an NTAccount identity
IdentityReference ntAccountIdentityReference = identity.Translate(typeof(NTAccount));
//get the value
string groupName = ntAccountIdentityReference.ToString();
//print to console
Console.WriteLine(groupName);
}
//pause
Console.ReadLine();
}
}
}
[VB.NET]
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Security.Principal
Imports System.Text
Namespace KeithRull.ActiveDirectory.IdentifyingUserGroups
Class Program
Private Shared Sub Main(ByVal args As String())
'get the WindowsIdentity of the current user
Dim currentWindowsIdentity As WindowsIdentity = WindowsIdentity.GetCurrent()
'retrieve the groups that the user belongs to
Dim currentWindowsIdentityGroups As IdentityReferenceCollection = currentWindowsIdentity.Groups
'iterate thru each group
For Each identity As IdentityReference In currentWindowsIdentityGroups
'translate the identity into an NTAccount identity
Dim ntAccountIdentityReference As IdentityReference = identity.Translate(GetType(NTAccount))
'get the value
Dim groupName As String = ntAccountIdentityReference.ToString()
'print to console
Console.WriteLine(groupName)
Next
'pause
Console.ReadLine()
End Sub
End Class
End Namespace
HTH
Posted
Sep 17 2008, 01:19 PM
by
keithrull