Exemple #1
0
 public static extern bool LogonUser(
     string lpszUsername,
     string lpszDomain,
     string lpszPassword,
     int dwLogonType,
     int dwLogonProvider,
     out LoggedOnUser phToken);
        public static LoggedOnUser LogonUser(string fulluserName, string password)
        {
            string username;
            string domain;
            SeparateUserAndDomain(fulluserName, out username, out domain);
            LoggedOnUser logonUserHandle = new LoggedOnUser();
            if (!SafeNativeMethods.LogonUser(username,
                domain,
                password,
                SafeNativeMethods.LOGON32_LOGON_NETWORK_CLEARTEXT,
                SafeNativeMethods.LOGON32_PROVIDER_DEFAULT,
                out logonUserHandle))
            {
                // no need to fail. Db sycn will fail
                return null;
            }

            logonUserHandle._userName = username;
            logonUserHandle._domain = domain;
            return logonUserHandle;
        }
        public static IDisposable ImpersonateUser(RemoteSystemInfo systemInfo)
        {
            Impersonator impersonator = new Impersonator();

            try
            {
                if (systemInfo != null)
                {
                    var loggedInUser = LoggedOnUser.LogonUser(systemInfo.Username, systemInfo.Password);
                    if (loggedInUser != null)
                    {
                        impersonator._impersonationContext = WindowsIdentity.Impersonate(loggedInUser.DangerousGetHandle());
                        impersonator._impersonatedUser     = loggedInUser.Username;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return(impersonator);
        }
Exemple #4
0
        public static LoggedOnUser LogonUser(string fulluserName, string password)
        {
            string username;
            string domain;

            SeparateUserAndDomain(fulluserName, out username, out domain);
            LoggedOnUser logonUserHandle = new LoggedOnUser();

            if (!SafeNativeMethods.LogonUser(username,
                                             domain,
                                             password,
                                             SafeNativeMethods.LOGON32_LOGON_NETWORK_CLEARTEXT,
                                             SafeNativeMethods.LOGON32_PROVIDER_DEFAULT,
                                             out logonUserHandle))
            {
                // no need to fail. Db sycn will fail
                return(null);
            }

            logonUserHandle._userName = username;
            logonUserHandle._domain   = domain;
            return(logonUserHandle);
        }
 public static extern bool LogonUser(
     string lpszUsername,
     string lpszDomain,
     string lpszPassword,
     int dwLogonType,
     int dwLogonProvider,
     out LoggedOnUser phToken);