Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Namespace ConsoleApplication12
' impersonation
Public Class ImpersonationExample
<DllImport("ADVAPI32")> _
Private Shared Function LogonUser(ByVal lpszUsername As [String], ByVal lpszDomain As [String], ByVal lpszPasswords As [String], ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByVal phToken As IntPtr) As Integer
End Function
Public Sub anyfunction()
Dim lToken As IntPtr
Dim lRetVal As Integer = 0
lRetval = LogonUser(strUserName, strDomain, strPassword, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, lToken)
If lRetval = 0 Then
Console.WriteLine("Login error occurred!")
Return
End If
Dim ImpId As New WindowsIdentity(lToken)
Dim ImpContext As WindowsImpersonationContext = ImpId.Impersonate()
' you are impersonating another user now in this
' thread!
' execute some managed and unmanaged code now
ImpContext.Undo()
End Sub
End Class
End Namespace