Esempio n. 1
0
 public void Dispose()
 {
     if (_context != null)
     {
         _context.UndoImpersonation();
         _context = null;
     }
 }
Esempio n. 2
0
        public static bool Run(Action action, LogonType logonType, string username, string domain, string password)
        {
            ImpersonationContext context = new ImpersonationContext();

            try
            {
                uint error = context.ImpersonateValidUser(logonType, username, domain, password);
                if (error == 0)
                {
                    action.Invoke();
                    return(true);
                }
            }
            finally
            {
                context.UndoImpersonation();
            }
            return(false);
        }