Check for administrator rights in VB .Net 2003

Here we go again with another usefull piece of code. The following code is for determining if the user that runs your application belongs to a specific group. In the following example i check if the user belongs to group ‘Administrators’

The following code applies in Namespace System.Security.Principal

Dim user As WindowsIdentity
user = WindowsIdentity.GetCurrent()
Dim principal As New WindowsPrincipal(user)
If Not principal.IsInRole(“BUILTIN\Administrators”)
MsgBox(“Sorry you have to be an administrator”)
End If

Ok that’s it. In VB .Net 2005 there is the My.User object which provides such functionality, so there is no need to do the above.

Happy coding 🙂

4 thoughts on “Check for administrator rights in VB .Net 2003”

  1. I’m not sure. I tested it in Windows 2003 server and it works. On other windows os i can’t tell. You should look for documentation in MSDN.

  2. this works for Administrator account in Vista but it can not work in scenario where user is member of “BUILTIN\Administrators” group

  3. What about localisation or if the “Administrators” group name has been changed? In the Dutch version of Windows the group is called “Administraturers” – or something like that.
    Will “BUILTIN\Administrators” still be found and actully point to the “Administraturers” group?

Comments are closed.