How (in VB.Net) do I find out if the logged in user is an administrator?
Printable View
How (in VB.Net) do I find out if the logged in user is an administrator?
If you mean local admin, you should be able to get what you need from this sample.
Check out where I invoke IsMember.
Code:Private Function AddUserToWinGroup(ByVal szGroup As String, ByVal szUser As String) As Boolean
Const MODULE_NAME = "AddUserToWinGroup"
Const ADSI_PROVIDER = "WinNT://"
Dim szPathGroup As String = ADSI_PROVIDER & m_szLocalMachine & "/" & szGroup & ",group"
' the username comes in looking like <domain>\<username>
' we need it to look like <domain>/<username> for ADSI to work
Dim szPathUser As String = ADSI_PROVIDER & szUser.Replace("\", "/") & ""
Try
Dim entryGroup As New DirectoryServices.DirectoryEntry(szPathGroup)
' Dim entryUser As New DirectoryServices.DirectoryEntry(szPathUser)
' only add the user if not already a member
If Not entryGroup.Invoke("ismember", szPathUser) Then
entryGroup.Invoke("add", szPathUser)
End If
Catch
m_szLastError = MODULE_NAME & ": " & Err.Description
Return False
End Try
Return True
End Function
VB Code:
Dim wp As New Security.Principal.WindowsPrincipal(Security.Principal.WindowsIdentity.GetCurrent) MsgBox(wp.IsInRole(Security.Principal.WindowsBuiltInRole.Administrator), , wp.Identity.Name) 'for none built in groups just use a string (domain\group) MsgBox(wp.IsInRole("MHC\PS Admins"), , wp.Identity.Name)