impersonating user

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
when I added code to impersonate a user it crashes my app when unning on a new test pc
also data from an access database no longer populates my list boxes
so first please help me to pass credentials to teh commands I need to run on remote machines , here is my code so far...
first a new class
<pre class="prettyprint lang-vb Imports System
Imports System.Security
Imports System.Security.Principal
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Public Class clsIMP
<DllImport("advapi32.dll")>
Private Shared Function LogonUser(ByVal lpszUsername As String, _
ByVal lpszDomain As String, _
ByVal lpszPassword As String, _
ByVal dwLogonType As Integer, _
ByVal dwLogonProvider As Integer, _
ByRef phToken As Integer) As Boolean
End Function
<DllImport("Kernel32.dll")>
Private Shared Function GetLastError() As Integer
End Function
Private Enum Logon
Interactive = 2
Network = 3
Batch = 4
Service = 5
Unlock = 7
NetworkCleartext = 8
NewCredentials = 9
End Enum
Private Enum Provider
UserDefault = 0
WindowsNT35 = 1
WindowsNT40 = 2
Windows2000 = 3
End Enum
Private NewContext As WindowsImpersonationContext
<SecurityPermission(SecurityAction.Demand, ControlPrincipal:=True, UnmanagedCode:=True)> _
Private Shared Function GetWindowsIdentity(ByVal Username As String, _
ByVal Domain As String, _
ByVal Password As String) As WindowsIdentity
Dim SecurityToken As Integer
Dim Success As Boolean
possible to extend program to allow changes to the logon type and provider
as Ineractive is slower and caches the information compared to the Logon.Network type.
Though that leaves open the private enumeration information.
Success = LogonUser(Username, Domain, Password, _
Logon.Network, Provider.UserDefault, _
SecurityToken)
If Not Success Then
Throw New System.Exception("Logon Failed. Error: " & GetLastError())
Err.Clear()
Else
GetWindowsIdentity = New WindowsIdentity(New IntPtr(SecurityToken))
End If
End Function
Public Function ImpersonateUser(ByVal username As String, _
ByVal domain As String, ByVal pwd As String) As Boolean
Dim NewIdentity As WindowsIdentity
Dim CurIdentity As WindowsIdentity
Try
NewIdentity = GetWindowsIdentity(username, domain, pwd)
If Not NewIdentity Is Nothing Then
NewContext = NewIdentity.Impersonate
CurIdentity = WindowsIdentity.GetCurrent
Debug.WriteLine("Impersonated ID: " & CurIdentity.Name) ‘used for demo/example
RemoveImpersonation()
just removing impersonation for demo/example
would comment out for actual use and call the
the RemoveImpersonation() method if all went well
else it gets called upon error event
CurIdentity = WindowsIdentity.GetCurrent used for demo/example
Debug.WriteLine("Logon ID: " & CurIdentity.Name) ‘used for demo/example
ImpersonateUser = True
Else
Err.Raise(7000, ImpersonateUser)
End If
Catch ex As Exception
RemoveImpersonation()
ImpersonateUser = False
Throw New System.Exception("IM Error: " & ex.Message)
Err.Clear()
End Try
Return ImpersonateUser
End Function
Public Function RemoveImpersonation() As Boolean
Try
If Not NewContext Is Nothing Then test if object was ever created/referenced
NewContext.Undo() if so, then undo impersonation.
RemoveImpersonation = True
Else
RemoveImpersonation = True never created object, so no impersonation to revert.
End If
Catch ex As Exception something happened during removal, so warn calling app to handle
RemoveImpersonation = False
Throw New System.Exception("Removal Failure: " & ex.Message)
Err.Clear()
End Try
Return RemoveImpersonation
End Function
End Class[/code]
then code to call it

<pre class="prettyprint lang-vb Dim objImpersonate As clsIMP = New clsIMP
objImpersonate.ImpersonateUser("my_username", "mydomain", "mypassword")[/code]
<br/>
Help pleeeeze! Oh masters of the VB.NET code <hr class="sig David Sheetz MCP

View the full article
 
Back
Top