2011-03-12 80 views
0

我有一个在Windows域中的服务器上自动生成的文件,称为“prod”,我需要让VB.NET将该文件传输到另一个文件服务器在另一个Windows域中,例如“QA”,其中QA和Prod完全具有不同的凭证,并且每次打开目标文件夹时都必须验证该凭证。使用VB.NET跨域文件复制

因此,我想常规filecopy方法将无法正常工作,有没有另一种方法可以实现这一目标?

非常感谢!

+3

不要在你的代码解决这个问题,存储的用户名/密码的你的代码或配置数据是根本上有缺陷的。让局域网管理员用外部信任对此进行分类,他可以在serverfault.com上询问它。 http://technet.microsoft.com/en-us/library/cc787646%28WS.10%29.aspx – 2011-03-12 17:56:45

回答

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") 
+0

这听起来很像,非常感谢! – AZhu 2011-03-12 18:18:19

+0

尝试运行代码时,实际上遇到了此错误:System.AccessViolationException被捕获 Message =“尝试读取或写入受保护的内存,这通常表示其他内存已损坏。”任何建议? – AZhu 2011-03-23 16:43:14

-1

令牌= Marshal.AllocHGlobal(8)