private static extern bool LogonUser( string principal, string authority, string password, LogonTypes logonType, LogonProviders logonProvider, out IntPtr token);
public static extern bool LogonUser( string principal, string authority, string password, LogonTypes logonType, LogonProviders logonProvider, ref SafeTokenHandle token);
/// <summary> /// Impersonates the specified user account. /// </summary> /// public void Impersonate(string userName, string domainName, string password, LogonTypes logonType, LogonProvider logonProvider) { UndoImpersonation(); IntPtr logonToken = IntPtr.Zero; IntPtr logonTokenDuplicate = IntPtr.Zero; try { // revert to the application pool identity, saving the identity of the current requestor _wic = WindowsIdentity.Impersonate(IntPtr.Zero); // do logon & impersonate if (Advapi32.LogonUser(userName, domainName, password, (int)logonType, (int)logonProvider, ref logonToken)) { if (Advapi32.DuplicateToken(logonToken, (int)SecurityImpersonationLevel.SecurityImpersonation, ref logonTokenDuplicate)) { var wi = new WindowsIdentity(logonTokenDuplicate); wi.Impersonate(); // discard the returned identity context (which is the context of the application pool) } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } } finally { if (logonToken != IntPtr.Zero) { Kernel32.CloseHandle(logonToken); } if (logonTokenDuplicate != IntPtr.Zero) { Kernel32.CloseHandle(logonTokenDuplicate); } } }
/// <summary> /// Impersonates the specified user account. /// </summary> /// public void Impersonate(string userName, string domainName, string password, LogonTypes logonType, LogonProvider logonProvider) { UndoImpersonation(); IntPtr logonToken = IntPtr.Zero; IntPtr logonTokenDuplicate = IntPtr.Zero; try { // revert to the application pool identity, saving the identity of the current requestor _wic = WindowsIdentity.Impersonate(IntPtr.Zero); // do logon & impersonate if (Advapi32.LogonUser(userName, domainName, password, (int)logonType, (int)logonProvider, ref logonToken)) { if (Advapi32.DuplicateToken(logonToken, (int)SecurityImpersonationLevel.SecurityImpersonation, ref logonTokenDuplicate)) { var wi = new WindowsIdentity(logonTokenDuplicate); wi.Impersonate(); // discard the returned identity context (which is the context of the application pool) } else throw new Win32Exception(Marshal.GetLastWin32Error()); } else throw new Win32Exception(Marshal.GetLastWin32Error()); } finally { if (logonToken != IntPtr.Zero) Kernel32.CloseHandle(logonToken); if (logonTokenDuplicate != IntPtr.Zero) Kernel32.CloseHandle(logonTokenDuplicate); } }
public ImpersonationHelper(LogonProvider logonProvider, ImpersonationLevel level, LogonTypes logonType) { this._logonProvider = logonProvider; this._impersonationLevel = level; this._logonType = logonType; }
internal static extern bool LogonUser([MarshalAs(UnmanagedType.LPStr)]string principal, [MarshalAs(UnmanagedType.LPStr)]string authority, [MarshalAs(UnmanagedType.LPStr)]string password, LogonTypes logonType, LogonProviders logonProvider, out IntPtr token);
/// <summary> /// Begins impersonation with the given credentials, Logon type and Logon provider. /// </summary> /// public Impersonator(string userName, string domainName, string password, LogonTypes logonType, LogonProvider logonProvider) { Impersonate(userName, domainName, password, logonType, logonProvider); }