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:
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:
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.
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.
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
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.
Originally Posted by crater
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.