2011-11-16 41 views
3

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

我正在使用adlpipi32.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(); 

} 

感谢....

+1

请向我们展示您的dll导入和实际调用。 – Dennis

+1

您是否使用runas命令测试了凭据? –

+0

在描述中添加的代码 – Denish

回答

1

,我有,做模拟类似的代码,但也有从你小的差异。这是从我的团队的其他开发人员传下来的,我确定它是从网上某处复制/粘贴的。它确实有效,我们在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); 
}