本文介绍了模拟跨网络文件副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to copy a file from remote machine in the same domain. so I am using impersonation to do that.

我使用ADVAPI32.DLL的dllimport的,它正确地模拟用户。

I am using DLLImport of advapi32.dll and it properly impersonate the user.

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

Now when below code line executed i got the following error.

\\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."



完整代码的要求

COMPLETE CODE AS REQUESTED

 [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服务和形式

The code that I have that does impersonation is similar, but there are small differences from yours. This was passed down from other developers at my group and I'm sure it's copy/paste from somewhere online. It does work though, and we use it in windows services and forms.

//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);
}

这篇关于模拟跨网络文件副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:50