Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 跨网络模拟文件拷贝_C#_Impersonation - Fatal编程技术网

C# 跨网络模拟文件拷贝

C# 跨网络模拟文件拷贝,c#,impersonation,C#,Impersonation,我想从同一域中的远程计算机复制一个文件。所以我使用模拟来实现这一点 我正在使用advapi32.dll的dlliport,它正确地模拟了用户 现在,当执行下面的代码行时,我得到了以下错误 \\line File.Copy(@"\\sins00048178\D$\BNCustody\Swift\Received_from_SWIFT\Error_files\E03248681_error.out", @"C:\E03248681_error.out", true); \\Error "Log

我想从同一域中的远程计算机复制一个文件。所以我使用模拟来实现这一点

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

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

\\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();

 }

提前感谢….

我拥有的模拟代码与您的类似,但与您的代码有一些小的差异。这是从我团队的其他开发人员那里传下来的,我确信这是从网上的某个地方复制/粘贴的。但它确实可以工作,我们在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);
}

请向我们展示您的dll导入和实际调用。您是否使用runas命令测试了凭据?@Denish此部分属于哪种应用程序?一些Windows服务/IIS?我想用Windows应用程序来做。@Denish你在Windows 2000下运行吗?