Results 1 to 11 of 11

Thread: Role of Current User

Hybrid View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    76

    Role of Current User

    I am running Visual Studio 2013 under Win 8 and moving my project from VB6 to vb.net

    I need to know whether the user is running as an Administrator (if not, then I will ask them to either do so or go and do something else)

    After the usual searching, I built this:

    Code:
      If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) = True Then
        MsgBox("User IS running as Administrator", MsgBoxStyle.SystemModal)
      End If
    
      If My.User.IsInRole(ApplicationServices.BuiltInRole.AccountOperator) = True  Then
        MsgBox("User IS running as AccountOperator", MsgBoxStyle.SystemModal)
      End If
    
      If My.User.IsInRole(ApplicationServices.BuiltInRole.BackupOperator) = True  Then
        MsgBox("User IS running as BackupOperator", MsgBoxStyle.SystemModal)
      End If
    
      If My.User.IsInRole(ApplicationServices.BuiltInRole.Guest) = True  Then
        MsgBox("User IS running as Guest", MsgBoxStyle.SystemModal)
      End If
    
      If My.User.IsInRole(ApplicationServices.BuiltInRole.PowerUser) = True  Then
        MsgBox("User IS running as PowerUser", MsgBoxStyle.SystemModal)
      End If
    
      If My.User.IsInRole(ApplicationServices.BuiltInRole.PrintOperator) = True  Then
        MsgBox("User IS running as PrintOperator", MsgBoxStyle.SystemModal)
      End If
    
      If My.User.IsInRole(ApplicationServices.BuiltInRole.Replicator) = True  Then
        MsgBox("User IS running as Replicator", MsgBoxStyle.SystemModal)
      End If
    
      If My.User.IsInRole(ApplicationServices.BuiltInRole.SystemOperator) = True  Then
        MsgBox("User IS running as SystemOperator", MsgBoxStyle.SystemModal)
      End If
    
      If My.User.IsInRole(ApplicationServices.BuiltInRole.User) = True  Then
        MsgBox("User IS running as User", MsgBoxStyle.SystemModal)
      End If
    But it produces nothing - even when I right click on the exe file and Run as Administrator. Anybody know why this does not work?

    I also found this routine which does produce output as per screen shot below:

    Code:
    Private Sub GetUserInfo()
      Dim UserIdentityInfo As System.Security.Principal.WindowsIdentity
      Dim strMsg           As String
    
      UserIdentityInfo = System.Security.Principal.WindowsIdentity.GetCurrent()
    
      strMsg = "User Name: " & UserIdentityInfo.Name & vbCrLf
    
      strMsg = strMsg & " Token: " & UserIdentityInfo.Token.ToString() & vbCrLf
    
      strMsg = strMsg & " AuthenticationType: " & UserIdentityInfo.AuthenticationType & vbCrLf
    
      strMsg = strMsg & " System: " & UserIdentityInfo.IsSystem & vbCrLf
    
      strMsg = strMsg & " Guest: " & UserIdentityInfo.IsGuest & vbCrLf
    
      strMsg = strMsg & " Anonymous: " & UserIdentityInfo.IsAnonymous & vbCrLf
    
      strMsg = strMsg & " User: " & UserIdentityInfo.User.ToString & vbCrLf
    
      strMsg = strMsg & " Owner: " & UserIdentityInfo.Owner.ToString & vbCrLf
    
      strMsg = strMsg & " Authenticated: " & UserIdentityInfo.IsAuthenticated.ToString & vbCrLf
    
      MsgBox(strMsg,MsgBoxStyle.SystemModal)
    
    End Sub
    Anybody know how to achieve my objective?
    Attached Images Attached Images  

  2. #2
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Role of Current User

    You should really think of using Select Case Statement or ElseIF.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    76

    Re: Role of Current User

    Quote Originally Posted by Toph View Post
    You should really think of using Select Case Statement or ElseIF.
    Well, yes but this was just a quick and dirty test and nothing is coming up!

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,250

    Re: Role of Current User

    So, you really mean that it produces NOTHING? You get no messagebox of any sort? That's a bit of a surprise. Did you put a breakpoint on the first if statement? If so, was it reached?

    I haven't worked with this, as I don't really care about the security roles as you are doing. However, I did find this page:

    http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx

    Consider the items pointed out in the Notes, especially the first note.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    76

    Re: Role of Current User

    Quote Originally Posted by Shaggy Hiker View Post
    So, you really mean that it produces NOTHING?
    Absolutely nothing

    Quote Originally Posted by Shaggy Hiker View Post
    You get no messagebox of any sort?
    None

    Quote Originally Posted by Shaggy Hiker View Post
    That's a bit of a surprise.
    I agree

    Quote Originally Posted by Shaggy Hiker View Post
    Did you put a breakpoint on the first if statement?
    Yes. I stepped through it line by line. I also run it as an exe file both as Admin and not as Admin. No output

    Quote Originally Posted by Shaggy Hiker View Post
    However, I did find this page:

    http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx

    Consider the items pointed out in the Notes, especially the first note.
    Thank you and noted

  6. #6
    Fanatic Member
    Join Date
    Aug 2005
    Posts
    606

    Re: Role of Current User

    Strange indeed, I copied your code into Form load event method of a new application and it works perfectly.

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    
            If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) = True Then
                MsgBox("User IS running as Administrator", MsgBoxStyle.SystemModal)
            End If
    
            If My.User.IsInRole(ApplicationServices.BuiltInRole.AccountOperator) = True Then
                MsgBox("User IS running as AccountOperator", MsgBoxStyle.SystemModal)
            End If
    
            If My.User.IsInRole(ApplicationServices.BuiltInRole.BackupOperator) = True Then
                MsgBox("User IS running as BackupOperator", MsgBoxStyle.SystemModal)
            End If
    
            If My.User.IsInRole(ApplicationServices.BuiltInRole.Guest) = True Then
                MsgBox("User IS running as Guest", MsgBoxStyle.SystemModal)
            End If
    
            If My.User.IsInRole(ApplicationServices.BuiltInRole.PowerUser) = True Then
                MsgBox("User IS running as PowerUser", MsgBoxStyle.SystemModal)
            End If
    
            If My.User.IsInRole(ApplicationServices.BuiltInRole.PrintOperator) = True Then
                MsgBox("User IS running as PrintOperator", MsgBoxStyle.SystemModal)
            End If
    
            If My.User.IsInRole(ApplicationServices.BuiltInRole.Replicator) = True Then
                MsgBox("User IS running as Replicator", MsgBoxStyle.SystemModal)
            End If
    
            If My.User.IsInRole(ApplicationServices.BuiltInRole.SystemOperator) = True Then
                MsgBox("User IS running as SystemOperator", MsgBoxStyle.SystemModal)
            End If
    
            If My.User.IsInRole(ApplicationServices.BuiltInRole.User) = True Then
                MsgBox("User IS running as User", MsgBoxStyle.SystemModal)
            End If
        End Sub
    End Class
    Life is about making some things happen, not waiting around for something to happen.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,250

    Re: Role of Current User

    Are you running in some kind of corporate environment where there are rules on your computer that you may not know about?
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    76

    Re: Role of Current User

    Quote Originally Posted by Shaggy Hiker View Post
    Are you running in some kind of corporate environment where there are rules on your computer that you may not know about?
    No, I am running it on my own personal HP Laptop in Main()

    I have just moved it to a Form Load event and walked through it line by line (it is NOT failing) but I still get no messages. Also run it as the exe and again I get no output

    Obviously, it works with a new application but is failing in mine for some reason ...

    Crater, are you using any Imports? I am not ...

    [Edit] Just tried creating a new project and it works in that - showing me as "User" but still does not produce any output in my real project
    Last edited by wavering; Dec 17th, 2014 at 04:20 AM.

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,571

    Re: Role of Current User

    Hello,

    Here is a simple class for AD information
    Code:
    Imports System.DirectoryServices
    Imports System.Security.Principal
    
    ''' <summary>
    ''' Provides basic information for the current user from Active Directory
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ADCurrentUser
        Private Shared _Instance As ADCurrentUser
        Private Shared currentADUser As AccountManagement.UserPrincipal
    
        Private Shared mInformationRetrived As Boolean
        ''' <summary>
        ''' Indicates if all information was instantiate from Active Directory
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property InformationRetrived As Boolean
            Get
                Return mInformationRetrived
            End Get
        End Property
        Private Shared mExceptionMessage As String
        ''' <summary>
        ''' Exception message if InformationRetrived returns false
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property ExceptionMessage As String
            Get
                Return mExceptionMessage
            End Get
        End Property
        Private Shared mUserName As String
        ''' <summary>
        ''' Returns user's first and last name
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property UserName As String
            Get
                Return mUserName
            End Get
        End Property
        Private Shared mFirstName As String
        ''' <summary>
        ''' Returns first name of user
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property FirstName As String
            Get
                Return mFirstName
            End Get
        End Property
        Private Shared mLastName As String
        ''' <summary>
        ''' Returns last name of user
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property LastName As String
            Get
                Return mLastName
            End Get
        End Property
        Private Shared mWelcomeMessage As String
        ''' <summary>
        ''' Canned welcome message
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property WelcomeMessage As String
            Get
                Return String.Concat("Welcome ", UserName)
            End Get
        End Property
        Private Shared mVoicePhoneNumber As String
        ''' <summary>
        ''' User's voice phone number
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property VoicePhoneNumber As String
            Get
                Return mVoicePhoneNumber
            End Get
        End Property
        Private Shared mEmailAddress As String
        ''' <summary>
        ''' User's email address
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property EmailAddress As String
            Get
                Return mEmailAddress
            End Get
        End Property
        Private Shared mSamAccountName As String
        ''' <summary>
        ''' User's Sam account name
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property SamAccountName As String
            Get
                Return mSamAccountName
            End Get
        End Property
        Private Shared mSid As String
        Public Shared ReadOnly Property SID As String
            Get
                Return mSid
            End Get
        End Property
        Private Shared mIsAdministrator As Boolean
        Public ReadOnly Property IsAdministrator As Boolean
            Get
                Return mIsAdministrator
            End Get
        End Property
        Private Shared mIsUser As Boolean
        Public ReadOnly Property IsUser As Boolean
            Get
                Return mIsUser
            End Get
        End Property
        Private Shared mIsGuest As Boolean
        Public ReadOnly Property IsGuest As Boolean
            Get
                Return mIsUser
            End Get
        End Property
        Private Shared mPowerUser As Boolean
        Public Shared ReadOnly Property IsPowerUser As Boolean
            Get
                Return mPowerUser
            End Get
        End Property
        Private Shared mIsAuthenticated As Boolean
        Public Shared ReadOnly Property IsAuthenticated As Boolean
            Get
                Return mIsAuthenticated
            End Get
        End Property
        Public Shared mAuthenticationType As String
        Public Shared ReadOnly Property AuthenticationType As String
            Get
                Return mAuthenticationType
            End Get
        End Property
    
        ''' <summary>
        ''' Instantiate information from AD.
        ''' This is done once.
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function Instance() As ADCurrentUser
    
            If _Instance Is Nothing Then
                Try
                    _Instance = New ADCurrentUser
                    currentADUser = AccountManagement.UserPrincipal.Current
                    mSid = currentADUser.Sid.ToString
                    mUserName = String.Concat(currentADUser.GivenName, " ", currentADUser.Surname.ProperCaseEnglish)
                    mFirstName = currentADUser.GivenName
                    mLastName = currentADUser.Surname.ProperCaseEnglish
                    mVoicePhoneNumber = currentADUser.VoiceTelephoneNumber
                    mEmailAddress = currentADUser.EmailAddress
                    mWelcomeMessage = String.Concat("Welcome ", mUserName)
                    mSamAccountName = currentADUser.SamAccountName
    
                    Dim p As New WindowsPrincipal(WindowsIdentity.GetCurrent())
    
                    Dim roles = System.Enum.GetValues(GetType(WindowsBuiltInRole)).Cast(Of WindowsBuiltInRole)()
                    For Each role In roles
                        Select Case role.ToString
                            Case "Administrator"
                                mIsAdministrator = p.IsInRole(role)
                            Case "User"
                                mIsUser = p.IsInRole(role)
                            Case "Guest"
                                mIsGuest = p.IsInRole(role)
                            Case "PowerUser"
                                mPowerUser = p.IsInRole(role)
                        End Select
                    Next
    
                    mIsAuthenticated = p.Identity.IsAuthenticated
                    mAuthenticationType = p.Identity.AuthenticationType
    
                    mInformationRetrived = True
                Catch ex As Exception
                    mInformationRetrived = False
                    mExceptionMessage = ex.Message
                End Try
            End If
            Return _Instance
    
        End Function
        Protected Sub New()
        End Sub
    End Class
    Extension methods
    Code:
    Module ExtensionMethods
        <System.Diagnostics.DebuggerStepThrough()>
        <Runtime.CompilerServices.Extension()>
        Public Function ProperCaseEnglish(ByVal sender As String) As String
            Return ProperCase(sender, "en-US")
        End Function
        <System.Diagnostics.DebuggerStepThrough()>
        <Runtime.CompilerServices.Extension()>
        Public Function ProperCase(ByVal sender As String, ByVal CultureCode As String) As String
            Dim TI As System.Globalization.TextInfo = New System.Globalization.CultureInfo(CultureCode, False).TextInfo
            Return TI.ToTitleCase(sender.ToLower)
        End Function
        <System.Diagnostics.DebuggerStepThrough()>
        <Runtime.CompilerServices.Extension()>
        Public Function GetValues(Of T)(sender As T) As IEnumerable(Of T)
            Return System.Enum.GetValues(GetType(T)).Cast(Of T)()
        End Function
    End Module
    Sample usage
    Code:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If ADCurrentUser.Instance.InformationRetrived Then
            txtFirstName.Text = ADCurrentUser.Instance.FirstName
            txtLastName.Text = ADCurrentUser.Instance.LastName
            txtPhone.Text = ADCurrentUser.Instance.VoicePhoneNumber
            txtEmail.Text = ADCurrentUser.Instance.EmailAddress
            txtFromEmail.Text = ADCurrentUser.Instance.EmailAddress
        End If
    End Sub
    Of course you can tweak as desired.

  10. #10
    Fanatic Member
    Join Date
    Aug 2005
    Posts
    606

    Re: Role of Current User

    So what is the method you are using this in? And do you have a try catch block surrounding your code?
    Life is about making some things happen, not waiting around for something to happen.

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,571

    Re: Role of Current User

    Quote Originally Posted by crater View Post
    So what is the method you are using this in?
    Where ever you want, it's a singleton pattern/static class so no matter where you access the members of the class its one instance.
    Quote Originally Posted by crater View Post
    And do you have a try catch block surrounding your code?
    The Try/Catch statement is optional, I had no issues but never know who will run this in a particular environment where the code could fail. Feel free to remove the statement if you like.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width