Олицетворение для копирования файлов по сети - PullRequest
3 голосов
/ 16 ноября 2011

Я хочу скопировать файл с удаленного компьютера в том же домене.поэтому я использую олицетворение, чтобы сделать это.

Я использую DLLImport файла advapi32.dll, и он правильно олицетворяет пользователя.

Теперь при выполнении строки кода ниже я получил следующую ошибку.

\\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 Ответ

1 голос
/ 12 декабря 2011

Код, который я использую для олицетворения, похож, но есть небольшие отличия от вашего.Это было передано от других разработчиков в моей группе, и я уверен, что это скопировать / вставить откуда-то онлайн.Это работает, хотя, и мы используем его в службах и формах 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);
}
...