问题
I have a file that is being generated automatically on a server in a Windows domain, say, called, "prod" and I will need to have VB.NET to transmit this file to another server in another Windows domain, say, "QA", where QA and Prod have different credentials altogether and I have to authenticate that credential every time I opened up the destination folder.
Therefore, I guess the regular filecopy method would not work, is there another way where we can accomplish this?
Thanks a lot!
回答1:
How about something like this
<DllImport("advapi32.dll")> _
Public Shared Function LogonUser(lpszUsername As String, lpszDomain As String, lpszPassword As String, dwLogonType As Integer, dwLogonProvider As Integer, phToken As IntPtr) As Boolean
End Function
<DllImport("kernel32.dll")> _
Public Shared Function CloseHandle(hObject As IntPtr) As Boolean
End Function
Public Shared Function OpenFileWithAccount(filename As String, username As String, domain As String, password As String) As Stream
Dim token As IntPtr
If Not LogonUser(username, domain, password, 2, 0, token) Then
Throw New Win32Exception()
End If
Try
Using WindowsIdentity.Impersonate(token)
Return File.OpenRead(filename)
End Using
Finally
CloseHandle(token)
End Try
End Function
and call
Dim stream as Stream
stream = OpenFileWithAccount("filePath","userName","prod","password")
回答2:
token = Marshal.AllocHGlobal(8)
来源:https://stackoverflow.com/questions/5283970/cross-domain-file-copy-using-vb-net