Cross domain file copy using VB.NET

*爱你&永不变心* 提交于 2019-12-25 08:59:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!