跨网络文件复制的模拟

本文关键字:模拟 复制 文件 网络 | 更新日期: 2023-09-27 18:14:41

我想从同一域中的远程机器复制一个文件。所以我用模仿来做到这一点。

我正在使用advapi32.dll的DLLImport,它正确地模拟了用户。

现在,当下面的代码行执行时,我得到了以下错误:

''line
File.Copy(@"''sins00048178'D$'BNCustody'Swift'Received_from_SWIFT'Error_files'E03248681_error.out", @"C:'E03248681_error.out", true);

''Error
"Logon failure: user not allowed to log on to this computer."

按要求完成代码

 [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool LogonUser(
        string lpszUsername,
        string lpszDomain,
        string lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        out IntPtr phToken
        );
IntPtr userHandle = IntPtr.Zero;
bool loggedOn = LogonUser(userid, domain, pass, 9, 0, out userHandle);
 if (loggedOn)
 {
    WindowsImpersonationContext context = WindowsIdentity.Impersonate(userHandle);
           File.Copy(@"''sins00048178'D$'BNCustody'Swift'Received_from_SWIFT'Error_files'E03248681_error.out", @"C:'E03248681_error.out", true);
    context.Undo();
 }

Thanks in advance....

跨网络文件复制的模拟

我拥有的模拟代码是类似的,但与您的代码有一些小区别。这是我们组的其他开发者传下来的,我敢肯定它是从网上复制/粘贴过来的。但它确实可以工作,我们在windows服务和窗体中使用它。

//defined elsewhere
WindowsImpersonationContext impersonatedUser;
WindowsIdentity newId;
IntPtr tokenHandle;
//Impersonate
tokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(userName, domainName, password, 2, 0, ref tokenHandle);
if (returnValue) {
    newId = new WindowsIdentity(tokenHandle);
    impersonatedUser = newId.Impersonate();
} else {
    //do some error handling
}
//Undo impersonation
if (impersonatedUser != null) {
    impersonatedUser.Undo();
}
if (tokenHandle != IntPtr.Zero) {
    CloseHandle(tokenHandle);
}