将文件从远程服务器复制到本地,反之亦然

时间:2011-11-21 10:22:10

标签: asp.net

感谢您的快速回复。但我只是坚持登录。我使用以下代码尝试登录:

bool success = LogonUser("username", "000.000.000.000", "#####",2 ,0 , out userToken);

但是因为失败而异常 即时通讯使用用户名和密码,我用来通过mstsc登录。

我编写代码将文件从远程机器复制到本地机器,反之亦然。 但我不知道如何指定凭据才能这样做.. 我的代码如下:

string sourceFile = System.IO.Path.Combine(globalInfo.GlobalServerPath, sourcePath ));
string destFile = System.IO.Path.Combine(globalInfo.GlobalPath, destPath)


string[] files = System.IO.Directory.GetFiles(sourceFile);

foreach (string s in files)
  {
   string fileName = System.IO.Path.GetFileName(s);
  fileName = System.IO.Path.Combine(destFile, fileName);
 System.IO.File.Copy(s, fileName, true);
 }

这里我收到错误:无法登录..

换行

string [] files = System.IO.Directory.GetFiles(sourceFile);

我正在连接远程机器。

请帮忙

2 个答案:

答案 0 :(得分:1)

你需要冒充:

 [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool LogonUser(
            string lpszUsername,
            string lpszDomain,
            string lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            out IntPtr phToken);

IntPtr userToken = IntPtr.Zero;

bool success = External.LogonUser(
  "john.doe", 
  "domain.com", 
  "MyPassword", 
  (int) AdvApi32Utility.LogonType.LOGON32_LOGON_INTERACTIVE, //2
  (int) AdvApi32Utility.LogonProvider.LOGON32_PROVIDER_DEFAULT, //0
  out userToken);

if (!success)
{
  throw new SecurityException("Logon user failed");
}

using (WindowsIdentity.Impersonate(userToken))
{
  // do the stuff with john.doe's credentials
}

答案 1 :(得分:0)

我们的项目中有类似的情况。我们在域上创建了一个用户,该用户在本地和远程服务器(同一域中的两个服务器)上都需要权限。我们有一个Windows服务以及配置为在此用户上下文下运行的Web应用程序。因此,在将文件复制到远程服务器或从远程服务器复制文件时,我们不会遇到权限问题。

但是,如果远程服务器不在同一个域上,则您可以使用模拟(如果可能)或在远程计算机上配置ftp服务器。然后,您可以使用ftp协议上载和下载文件。

相关问题