Detect Users Group

Posted on 2006-04-19
Last Modified: 2010-04-23

Anyone know how to detect which group the user is in?  For example as a test, I would like the program to show a messagebox when it loads to show which group he or she is in.  So for me it should show "Power User", for my manager it should say Domain Admin, for a normal user is should say user etc etc.

Anyone know if this can be done?

Question by:narmi2
    LVL 4

    Expert Comment

    Are you looking check if a user is in a specific group or are you looking for a list of groups the user is in?
    LVL 96

    Accepted Solution

    Here is a class to get current user roles:

    Imports System.Reflection
    Imports System.Security.Principal

    Public Class SecurityRoles

      Public Shared Function GetRoles() As String()

        Dim id As WindowsIdentity = WindowsIdentity.GetCurrent()

        Dim idType As Type
        idType = GetType(WindowsIdentity)
        Dim result As String() = idType.InvokeMember("_GetRoles", _
         BindingFlags.Static Or BindingFlags.InvokeMethod Or BindingFlags.NonPublic, _
         Nothing, id, New Object() {id.Token}, Nothing)

        Return result

      End Function

    End Class

    LVL 4

    Assisted Solution

    If your goal was to detect the group for the user and put it in a message box you can use the function above along with the following code to get the job done:

            Dim i As Integer
            Dim mystring As String

            i = 0

            Do While i <= (GetRoles.Length - 1)
                mystring += GetRoles.GetValue(i) & Chr(10)

                i += 1



    However, I wanted to know and suspect that you would like to check if the user belongs to a specific group.  This can be accomplished by using the following:

    Imports System.Security.Principal
    Imports System.Security.Permissions

    Imports System.Threading

       ' <Description("Please use 1 for the optional parameter to specify a builtin role")> _
            Public Function InRole(ByVal mRole As String, Optional ByVal mBuiltIn As Integer = 0) As Boolean  'Set to


                If Not (mBuiltIn = 1) Then
                        Dim p1 As PrincipalPermission = New PrincipalPermission(Thread.CurrentThread.CurrentPrincipal.Identity.Name, mstrDomain & "\" & mRole)  '<--- mstrDomain needs to be set to your computer name or Domain name before you call this function.

                        Return True

                    Catch ex As Exception

                        Return False

                    End Try


                        Dim p1 As PrincipalPermission = New PrincipalPermission(Thread.CurrentThread.CurrentPrincipal.Identity.Name, "BUILTIN\" & mRole)

                        Return True

                    Catch ex As Exception

                        Return False

                    End Try

                End If

            End Function

    All you need to do is call the function like so to verify if the user is in the proper role:

     If Not ((InRole("BANKER")) Or _
                InRole("Administrators", 1)) Then

                MsgBox("You don't have permission to take Money!", MsgBoxStyle.Critical, "Security Restriction!")

                ' Add code here to disable or enable secure part of program

            End If

    Please let me know if you have any questiions.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now