2011-11-16 1 views
3

동일한 도메인의 원격 시스템에서 파일을 복사하려고합니다. 그래서 나는 그것을하기 위해 가장을 사용하고 있습니다.네트워크를 통한 파일 복사의 가장.

나는 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(); 

} 

감사합니다 ....

+1

dll 가져 오기와 실제 호출을 알려주십시오. – Dennis

+1

runas 명령으로 자격 증명을 테스트 해 보셨습니까? –

+0

코드가 description에 추가되었습니다. – 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); 
}