我想从同一域中的远程计算机复制文件。所以我正在冒充这样做。
我正在使用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();
}
提前致谢....
答案 0 :(得分: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);
}