Exemple #1
0
        public static Identity Impersonate(string suser, string spwd)
        {
            Identity id = new Identity();

            if (string.IsNullOrEmpty(suser) || string.IsNullOrEmpty(spwd))
            {
                try
                {
                    id._selfContext = WindowsIdentity.Impersonate(IntPtr.Zero); // REVERT to AppPool identity!
                    id._context     = AppPoolIdentity.Impersonate();
                }
                catch
                {
                    id.UndoImpersonation();
                    throw;
                }
            }
            else
            {
                try
                {
                    id._selfContext = WindowsIdentity.Impersonate(IntPtr.Zero); // REVERT to AppPool identity!
                    id._context     = UserIdentity(suser, spwd).Impersonate();
                }
                catch
                {
                    id.UndoImpersonation();
                    throw;
                }
            }
            return(id);
        }
        /// <summary>
        /// Executes a supplied delegate while impersonating the application pool
        /// account.
        /// </summary>
        ///
        /// <param name="del">The delegate to execute.</param>
        ///
        internal static void ImpersonateAppPool(VoidDelegate del)
        {
            try
            {
                WindowsImpersonationContext m_context = null;
                try
                {
#if false
                    m_context = AppPoolIdentity.Impersonate();
#else
                    m_context = WindowsIdentity.Impersonate(IntPtr.Zero);
#endif
                    del();
                }
                finally
                {
                    if (m_context != null)
                    {
                        m_context.Dispose();
                    }
                }
            }
            catch
            {
                // prevent exception filter exploits
                throw;
            }
        }
Exemple #3
0
 private Identity()
 {
     try
     {
         this.selfContext = WindowsIdentity.Impersonate(IntPtr.Zero);
         this.context     = AppPoolIdentity.Impersonate();
     }
     catch
     {
         this.UndoImpersonation();
         throw;
     }
 }