2017-05-01 12 views
0

다른 컴퓨터에 내 컨트롤러에 로그온하고 파일을 복사해야합니다. 원격 컴퓨터에서 로컬 사용자를 사용해야합니다.다른 컴퓨터에서 로컬 사용자 가장

using (Impersonate.LogonUser(true, 
     ".", 
     "todev1.domain.com\admin", 
     "Test123_", 
     LogonType.Interactive)) 
    { 

    } 

이 승리 API :이 인수 전달

private Impersonate(bool active, string domain, string username, string password, LogonType logonType) 
    { 
     if (active) 
     { 
      IntPtr handle; 
      var ok = NativeMethods.LogonUser(username, domain, password, (int)logonType, 0, out handle); 
      if (!ok) 
      { 
       var errorCode = Marshal.GetLastWin32Error(); 
       throw new ApplicationException(string.Format("Could not impersonate the elevated user. LogonUser returned error code {0}.", errorCode)); 
      } 

      _handle = new SafeTokenHandle(handle); 
      _context = WindowsIdentity.Impersonate(_handle.DangerousGetHandle()); 
     } 
    } 

:

현재는이 코드를 사용하고 나는이 질문을 확인

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
internal static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken); 

를/A Using advapi32.dll:LogonUserA() to impersonate a remote machine's local user하지만, 제공되는 솔루션이 작동하지 않습니다.

나는 여러 값을 도메인, 사용자 등으로 메서드에 전달하려고 시도했지만 엄밀한 솔루션을 찾을 수 없습니다. NewCredentials을 사용해 보았지만 로깅되지 않은 경우에도 항상 반환됩니다.

답변

0

마지막으로 원격 컴퓨터로 가장 할 각 컴퓨터에 사용자를 추가 할 필요없이이 문제를 해결했습니다.

NewCredential을 사용하지만 WINNT50 LogonProvider를 사용하는 것이 맞습니다.

그런 다음
private Impersonate(bool active, string domain, string username, string password, LogonType logonType, LogonProvider logonProvider) 
     { 
      if (active) 
      { 
       IntPtr handle; 
       var ok = NativeMethods.LogonUser(username, domain, password, (int)logonType, (int)logonProvider, out handle); 
       if (!ok) 
       { 
        var errorCode = Marshal.GetLastWin32Error(); 
        throw new ApplicationException(string.Format("Could not impersonate the elevated user. LogonUser returned error code {0}.", errorCode)); 
       } 

       _handle = new SafeTokenHandle(handle); 
       _context = WindowsIdentity.Impersonate(_handle.DangerousGetHandle()); 
      } 
     } 

내가 코드를 사용하여로 가장 메소드를 호출 : :

그래서 내 가장 할 방법은 지금처럼

using (Impersonate.LogonUser(true, 
    "todev1.domain.com", 
    "admin", 
    "Test123_", 
    LogonType.NewCredentials, 
    LogonProvider.WinNT50)) 
{ 

} 
+0

LOGON32_PROVIDER_WINNT50만으로는 작동하지 않습니다. 심지어 몇 가지 로그온 유형을 시도했습니다. 원격 컴퓨터에서 로컬 사용자 인 원격 폴더에 액세스하려고 할 때 "사용자 또는 암호가 잘못되었습니다."라는 메시지가 계속 나타납니다. 도메인이 전달 된 경우 LOGON32_PROVIDER_WINNT50은 LOGON32_PROVIDER_DEFAULT와 동일하게 처리됩니다 (문서에 따라). 코드가 실제로 작동하면 공유 한 코드에 표시되지 않는 다른 것이 있어야합니다. 전체 코드와 호출을 공유 할 수 있었습니까? –

+0

@ JürgenBayer가 가장 방법을 사용하는 코드를 추가했습니다. 코드는 정상적으로 작동합니다. –

0

원격 컴퓨터의 로컬 사용자와 동일한 사용자 이름과 암호로 로컬 컴퓨터에서 로컬 사용자를 만들 수 있습니다.

+0

감사합니다,하지만 난의 각 시스템에 사용자를 복제 할 수 없습니다 농장; 내 대답을 보면 해결책을 찾지 못했다. –