using System; using System.Collections.Generic; using System.Text; using System.Security.Principal; using System.Runtime.InteropServices; namespace ClipSoft.Utility { public class ImpersonationUtility { /////////////////////////////////////////////////////////////////////// // Impersonate Method : BasePage에서 내용을 Copy함 /////////////////////////////////////////////////////////////////////// #region Impersonation /// /// LogonUser정보처리 /// /// lpszUsername /// lpszDomain /// lpszPassword /// dwLogonType /// dwLogonProvider /// phToken /// bool [DllImport("advapi32.dll", SetLastError = true)] public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); /// /// CloseHandle /// /// handle /// bool [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern bool CloseHandle(IntPtr handle); /// /// DuplicateToken /// /// ExistingTokenHandle /// SECURITY_IMPERSONATION_LEVEL /// DuplicateTokenHandle /// bool [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle); WindowsImpersonationContext _imp_user; /// /// Impersonation을 실행합니다. /// /// 도메인 /// 사용자아이디 /// 비밀번호 /// public bool ImpersonationStart(string myDomain, string userid, string pwd) { try { IntPtr token = IntPtr.Zero; IntPtr dupe_token = IntPtr.Zero; string domain = myDomain; string user_id = userid; string password = pwd; WindowsIdentity ident = null; int error_code = 0; bool result = LogonUser(user_id, domain, password, 2, 0, ref token); if (!result) { error_code = Marshal.GetLastWin32Error(); throw new Exception("Impersonation 로그인 실패 하였습니다. 오류코드 ->" + Convert.ToString(error_code)); } result = DuplicateToken(token, 2, ref dupe_token); if (!result) { CloseHandle(token); throw new Exception("Impersonation Duplicate시 오류가 발생하였습니다."); } ident = new WindowsIdentity(dupe_token); _imp_user = ident.Impersonate(); return true; } catch (Exception ex) { } return false; } /// /// Impersonation을 종료합니다. /// public void ImpersonationEnd() { if (_imp_user != null) _imp_user.Undo(); } #endregion } }