Esempio n. 1
0
 private static extern bool LogonUser(
     string principal,
     string authority,
     string password,
     LogonTypes logonType,
     LogonProviders logonProvider,
     out IntPtr token);
Esempio n. 2
0
 private static extern bool LogonUser(
     string principal,
     string authority,
     string password,
     LogonTypes logonType,
     LogonProviders logonProvider,
     out IntPtr token);
Esempio n. 3
0
 public static extern bool LogonUser(
     string principal,
     string authority,
     string password,
     LogonTypes logonType,
     LogonProviders logonProvider,
     ref SafeTokenHandle token);
Esempio n. 4
0
        /// <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);
                }
            }
        }
Esempio n. 5
0
        /// <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;
		}
Esempio n. 7
0
 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);
Esempio n. 8
0
 /// <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);
 }
Esempio n. 9
0
 /// <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);
 }
Esempio n. 10
0
 public static extern bool LogonUser(
   string principal,
   string authority,
   string password,
   LogonTypes logonType,
   LogonProviders logonProvider,
   ref SafeTokenHandle token);