我有一个在Windows域中的服务器上自动生成的文件,比如称为“prod”,我需要让VB.NET将此文件传输到另一个Windows域中的另一台服务器,比方说, “QA”,QA和Prod完全有不同的凭据,每次打开目标文件夹时都必须验证该凭证。
因此,我猜常规的filecopy方法不起作用,还有另一种方法可以实现这个目的吗?
非常感谢!
答案 0 :(得分:1)
这样的事情
<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
并致电
Dim stream as Stream
stream = OpenFileWithAccount("filePath","userName","prod","password")
答案 1 :(得分:-1)
token = Marshal.AllocHGlobal(8)