K
Kroese, Ramon
Hi,
I'm trying to (re)write a function in VB.NET (VS2008) which determines if
the current user has administrator privileges on a remote computer and
returns a boolean. I want to determine this before calling an external
program. If possible I do not want to enumerate the members of the local
administrators group with nesting to one or more active directory security
groups.
In VB6 I used the returncode of an NetWkstaGetInfo API call to determine the
administrator privileges but I could not get this to work in vb.net.
Declare Function NetWkstaGetInfo Lib "Netapi32" (ByVal servername As
String, ByVal level As Long, ByVal lpBuf As Long) As Long
Declare Function NetApiBufferFree Lib "NETAPI32.DLL" (ByVal Ptr As Long)
As Long
Public Function IsRemoteAdmin(ByVal strComputername As String) As
Boolean
Dim lResult As Long
Dim pWrkInfo As Long
IsRemoteAdmin = False
Dim strServername As String = "\\" & strComputername & vbNullString
lResult = NetWkstaGetInfo(strServername , 102, pWrkInfo)
If lResult = 0 Then
NetApiBufferFree(pWrkInfo)
IsRemoteAdmin = True
ElseIf lResult = 5 Then
IsRemoteAdmin = False
End If
End Function
To determine if the current user has administrative rights on the local
computer I now use WindowsPrincipal and
IsInRole(WindowsBuiltInRole.Administrator).
Public Function IsAdmin() As Boolean
Dim wp As New WindowsPrincipal(WindowsIdentity.GetCurrent())
IsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator)
End Function
Probably there is an better method in VB.NET to detect if the current user
has administrator privileges on a remote computer but I can not find it. Can
anyone provide me an working function?
Regards,
R. Kroese
I'm trying to (re)write a function in VB.NET (VS2008) which determines if
the current user has administrator privileges on a remote computer and
returns a boolean. I want to determine this before calling an external
program. If possible I do not want to enumerate the members of the local
administrators group with nesting to one or more active directory security
groups.
In VB6 I used the returncode of an NetWkstaGetInfo API call to determine the
administrator privileges but I could not get this to work in vb.net.
Declare Function NetWkstaGetInfo Lib "Netapi32" (ByVal servername As
String, ByVal level As Long, ByVal lpBuf As Long) As Long
Declare Function NetApiBufferFree Lib "NETAPI32.DLL" (ByVal Ptr As Long)
As Long
Public Function IsRemoteAdmin(ByVal strComputername As String) As
Boolean
Dim lResult As Long
Dim pWrkInfo As Long
IsRemoteAdmin = False
Dim strServername As String = "\\" & strComputername & vbNullString
lResult = NetWkstaGetInfo(strServername , 102, pWrkInfo)
If lResult = 0 Then
NetApiBufferFree(pWrkInfo)
IsRemoteAdmin = True
ElseIf lResult = 5 Then
IsRemoteAdmin = False
End If
End Function
To determine if the current user has administrative rights on the local
computer I now use WindowsPrincipal and
IsInRole(WindowsBuiltInRole.Administrator).
Public Function IsAdmin() As Boolean
Dim wp As New WindowsPrincipal(WindowsIdentity.GetCurrent())
IsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator)
End Function
Probably there is an better method in VB.NET to detect if the current user
has administrator privileges on a remote computer but I can not find it. Can
anyone provide me an working function?
Regards,
R. Kroese