I have a file that is residing on a different domain than my program. My
program needs to open up that file. The issue is that I somehow need to
provide the username and password for the domain. How can I do this?
I attached my code so far but this is not working.
Thanks,
Chris
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Public Class Form1
Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal
lpszUsername As [String], _
ByVal lpszDomain As [String], ByVal lpszPassword As [String], _
ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
ByRef phToken As IntPtr) As Boolean
Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal
handle As IntPtr) As Boolean
Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Const LOGON32_LOGON_INTERACTIVE As Integer = 2 ' This is so LogonUser
will create a primary token
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Create a token that represents the new user identity
Dim tokenHandle As New IntPtr(0)
tokenHandle = IntPtr.Zero
' Call LogonUser to obtain a handle to an access token.
Dim usr As String = "admin"
Dim pwd As String = "pwd"
Dim dom As String = "webdomain"
Dim returnValue As Boolean = LogonUser(usr, dom, pwd,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, tokenHandle)
If Not returnValue Then
MsgBox("Error # " & Marshal.GetLastWin32Error().ToString)
End
Else
MsgBox("Success")
End If
' Now we use that token to create ourselves a new windows identity
Dim newId As New WindowsIdentity(tokenHandle)
' And temporarily impersonate said user
Dim impersonatedUser As WindowsImpersonationContext =
newId.Impersonate()
Process.Start("\\111.111.11.11\c$\Apps\test.jpg")
' Stop impersonating the user
impersonatedUser.Undo()
' After you're done, good idea to cleanup and free the token
If Not System.IntPtr.op_Equality(tokenHandle, IntPtr.Zero) Then
CloseHandle(tokenHandle)
End If
End Sub
End Class