I know this is an old thread but I have a piece of code that I needed once that will do just what you need!
Code:
Public Class Impersonator
Implements IDisposable
Private Context As WindowsImpersonationContext = Nothing
Public Sub New(userName As String, password As String, domainName As String)
Impersonate(userName, password, domainName)
End Sub
Public Sub Dispose()
UndoImpersonation()
End Sub
<DllImport("advapi32.dll", SetLastError := True)> _
Private Shared Function LogonUser(lpszUserName As String, lpszDomain As String, lpszPassword As String, dwLogonType As Integer, dwLogonProvider As Integer, ByRef phToken As IntPtr) As Integer
End Function
<DllImport("advapi32.dll", CharSet := CharSet.Auto, SetLastError := True)> _
Private Shared Function DuplicateToken(hToken As IntPtr, impersonationLevel As Integer, ByRef hNewToken As IntPtr) As Integer
End Function
<DllImport("advapi32.dll", CharSet := CharSet.Auto, SetLastError := True)> _
Private Shared Function RevertToSelf() As Boolean
End Function
<DllImport("kernel32.dll", CharSet := CharSet.Auto)> _
Private Shared Function CloseHandle(handle As IntPtr) As Boolean
End Function
Private Const LOGON32_LOGON_INTERACTIVE As Integer = 2
Private Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Private Sub Impersonate(userName As String, password As String, domain As String)
Dim tempoaryIdentity As WindowsIdentity = Nothing
Dim token As IntPtr = IntPtr.Zero
Dim tokenDup As IntPtr = IntPtr.Zero
Try
If RevertToSelf() Then
If LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
If DuplicateToken(token, 2, tokenDup) <> 0 Then
tempoaryIdentity = New WindowsIdentity(tokenDup)
impersonationContext = tempoaryIdentity.Impersonate()
Else
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
Else
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
Else
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
Finally
If token <> IntPtr.Zero Then
CloseHandle(token)
End If
If tokenDuplicate <> IntPtr.Zero Then
CloseHandle(tokenDuplicate)
End If
End Try
End Sub
Private Sub UndoImpersonation()
If Context IsNot Nothing Then
Context.Undo()
End If
End Sub
End Class
You can then use it like this:
Code:
Using New Impersonator("Username", "Password", "DomainName")
[ Code here runs as the above user ]
End Using
To obtain the Username, Password and Domain name of a certain user you can use the CredUI API of windows and call the appropriate
[Only registered and activated users can see links. Click Here To Register...] or the
[Only registered and activated users can see links. Click Here To Register...] functions.
Some example code I grabbed from StackOverflow follows:
Code:
''' <summary>
''' Leverages the windows UI to prompt for a password
''' </summary>
Friend NotInheritable Class Authentication
Private Sub New()
End Sub
Public Structure CREDUI_INFO
Public cbSize As Integer
Public hwndParent As IntPtr
Public pszMessageText As String
Public pszCaptionText As String
Public hbmBanner As IntPtr
End Structure
<DllImport("credui")> _
Private Shared Function CredUIPromptForCredentials(ByRef creditUR As CREDUI_INFO, targetName As String, reserved1 As IntPtr, iError As Integer, userName As StringBuilder, maxUserName As Integer, _
password As StringBuilder, maxPassword As Integer, <MarshalAs(UnmanagedType.Bool)> ByRef pfSave As Boolean, flags As CREDUI_FLAGS) As CredUIReturnCodes
End Function
<Flags> _
Private Enum CREDUI_FLAGS
INCORRECT_PASSWORD = &H1
DO_NOT_PERSIST = &H2
REQUEST_ADMINISTRATOR = &H4
EXCLUDE_CERTIFICATES = &H8
REQUIRE_CERTIFICATE = &H10
SHOW_SAVE_CHECK_BOX = &H40
ALWAYS_SHOW_UI = &H80
REQUIRE_SMARTCARD = &H100
PASSWORD_ONLY_OK = &H200
VALIDATE_USERNAME = &H400
COMPLETE_USERNAME = &H800
PERSIST = &H1000
SERVER_CREDENTIAL = &H4000
EXPECT_CONFIRMATION = &H20000
GENERIC_CREDENTIALS = &H40000
USERNAME_TARGET_CREDENTIALS = &H80000
KEEP_USERNAME = &H100000
End Enum
Public Enum CredUIReturnCodes
NO_ERROR = 0
ERROR_CANCELLED = 1223
ERROR_NO_SUCH_LOGON_SESSION = 1312
ERROR_NOT_FOUND = 1168
ERROR_INVALID_ACCOUNT_NAME = 1315
ERROR_INSUFFICIENT_BUFFER = 122
ERROR_INVALID_PARAMETER = 87
ERROR_INVALID_FLAGS = 1004
End Enum
''' <summary>
''' Prompts for password.
''' </summary>
''' <param name="user">The user.</param>
''' <param name="password">The password.</param>
''' <returns>True if no errors.</returns>
Friend Shared Function PromptForPassword(user As String, password As String) As Boolean
' Setup the flags and variables
Dim userPassword As New StringBuilder(), userID As New StringBuilder()
Dim credUI As New CREDUI_INFO()
credUI.cbSize = Marshal.SizeOf(credUI)
Dim save As Boolean = False
Dim flags As CREDUI_FLAGS = CREDUI_FLAGS.ALWAYS_SHOW_UI Or CREDUI_FLAGS.GENERIC_CREDENTIALS
' Prompt the user
Dim returnCode As CredUIReturnCodes = CredUIPromptForCredentials(credUI, Application.ProductName, IntPtr.Zero, 0, userID, 100, _
userPassword, 100, save, flags)
user = userID.ToString()
password = userPassword.ToString()
Return (returnCode = CredUIReturnCodes.NO_ERROR)
End Function
End Class
Hope its what you need! -jD