Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
 Wednesday, September 17, 2008

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

 

Wednesday, September 17, 2008 8:21:38 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | ActiveDirectory
Archive
<December 2008>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Keith Rull
Sign In
Statistics
Total Posts: 260
This Year: 57
This Month: 0
This Week: 0
Comments: 116
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)