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 🙂
Will this work on all types of os.
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.
this works for Administrator account in Vista but it can not work in scenario where user is member of “BUILTIN\Administrators” group
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?