public void GetServices(BO.Host host) { host.Services = new List <BO.Service>(); try { ImpersonateUser iu = new ImpersonateUser(); if (!(string.IsNullOrEmpty(host.Username) || string.IsNullOrEmpty(host.Password))) { iu.Impersonate(host.Domain, host.Username, AES.DecryptString(host.Password)); } System.ServiceProcess.ServiceController[] sc = System.ServiceProcess.ServiceController.GetServices(host.Value); foreach (var s in sc) { host.Services.Add(new BO.Service { Name = s.DisplayName, Value = s.ServiceName, Status = s.Status }); } iu.Undo(); } catch (Exception) { //OnLog(new BO.LogEventArgs(string.Format("Failed to Connect to Server: {0} [{1}]\r\n", host.Name, host.Value ))); } }
bool WindowsStop() { ImpersonateUser iu = new ImpersonateUser(); try { if (!(string.IsNullOrEmpty(this.Username) || string.IsNullOrEmpty(this.Password))) { iu.Impersonate(this.Domain, this.Username, AES.DecryptString(this.Password)); } using (ServiceController sc = new ServiceController(this.Value, this.Host)) { OnLog(new LogEventArgs(string.Format("Stopping {0} on {1}", this.Name, this.Server))); if (sc.Status == ServiceControllerStatus.Stopped) { OnLog(new LogEventArgs(string.Format("Service {0} already stopped on {1}", this.Name, this.Server))); OnProgress(new EventArgs()); return(true); } sc.Stop(); sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, this.TimeOut)); sc.Refresh(); OnLog(new LogEventArgs(string.Format("[{2}] - {0} - {1}", this.Name, sc.Status, this.Server))); OnProgress(new EventArgs()); Thread.Sleep(this.StopDelay * 1000); return(sc.Status == ServiceControllerStatus.Stopped); } } finally { iu.Undo(); } }
private void Impersonate() { if (UserName.IsNullOrEmpty() || Password.IsNullOrEmpty()) { return; } ImpersonationHandler = new ImpersonateUser(); var domainUser = UserName.Split('\\'); var userPart = UserName; var domainPart = ""; if (domainUser.Length == 2) { userPart = domainUser[1]; domainPart = domainUser[0]; } ImpersonationHandler.Impersonate(domainPart, userPart, Password); }
static void Main(string[] args) { ///////////////////////////////////////////////////////////////////// // Gather the credential information of the impersonated user. // Console.WriteLine("Before impersonation ..."); Console.WriteLine("Current user is {0}", WindowsIdentity.GetCurrent().Name); Console.WriteLine("Input user name"); string username = Console.ReadLine(); Console.WriteLine("Input domain name"); string domain = Console.ReadLine(); Console.WriteLine("Input password"); SecureString password = ImpersonateUser.GetPassword(); ///////////////////////////////////////////////////////////////////// // Impersonate the specified user. The impersonation is automatically // undone after the Impersonate method. // ImpersonateUser.Impersonate <object, object>(username, domain, password, null, delegate { Console.WriteLine("\nDuring impersonation ..."); Console.WriteLine("Current user is {0}", WindowsIdentity.GetCurrent().Name); // Do the work as the user ... return(null); }); Console.WriteLine("\nAfter impersonation is undone ..."); Console.WriteLine("Current user is {0}", WindowsIdentity.GetCurrent().Name); }
private void drpbx_IngestionSource_SelectedValueChanged(object sender, EventArgs e) { bool result = true; SQLFunction sqlfunc = new SQLFunction(); lstbx_MediaType.Items.Clear(); lstbx_Publisher.Items.Clear(); if (Grd_MetaDataPub.ColumnCount > 0) { Grd_MetaDataPub.DataSource = null; Grd_MetaDataPub.Columns.Clear(); btn_Injestion.BackColor = Color.DimGray; btn_Injestion.Enabled = false; btn_Injestion.Refresh(); System.Windows.Forms.Application.DoEvents(); btn_Ingestion2.BackColor = Color.DimGray; btn_Ingestion2.Enabled = false; btn_Ingestion2.Refresh(); System.Windows.Forms.Application.DoEvents(); btn_ErrorLog.BackColor = Color.DimGray; btn_ErrorLog.Enabled = false; btn_ErrorLog.Refresh(); System.Windows.Forms.Application.DoEvents(); } if (drpbx_IngestionSource.SelectedValue.ToString() == "Recorded Books") { lbl_CompanySelected.Text = "RB"; lbl_CompanySelected.Refresh(); System.Windows.Forms.Application.DoEvents(); ErrorLogCount("RB"); Check_TitleCount("RB"); btn_RoyaltyRateDiscount.Visible = true; btn_RoyaltyRateDiscount.Enabled = true; lbl_Message.Text = ""; #region 'Populate the MediaType' result = sqlfunc.PopulateMediaType("RB", lstbx_MediaType); #endregion } else if (drpbx_IngestionSource.SelectedValue.ToString() == "W F Howes") { lbl_CompanySelected.Text = "WFH"; lbl_CompanySelected.Refresh(); System.Windows.Forms.Application.DoEvents(); if (System.Configuration.ConfigurationManager.AppSettings["Platform"].ToString().ToLower() != "dev") { ImpersonateUser iU = new ImpersonateUser(); // TODO: Replace credentials iU.Impersonate(); } ErrorLogCount("WFH"); Check_TitleCount("WFH"); btn_RoyaltyRateDiscount.Visible = false; btn_RoyaltyRateDiscount.Enabled = false; lbl_Message.Text = ""; #region 'Populate the MediaType' result = sqlfunc.PopulateMediaType("WFH", lstbx_MediaType); #endregion } else { lbl_CompanySelected.Text = ""; lbl_CompanySelected.Refresh(); System.Windows.Forms.Application.DoEvents(); lbl_Message.Text = ""; lstbx_MediaType.Items.Clear(); lstbx_Publisher.Items.Clear(); } if (!result) { lbl_Message.Text = "There has been some problem with your request. Please Check the Error Logs"; } }
private void Impersonate() { if (UserName.IsNullOrEmpty() || Password.IsNullOrEmpty()) return; ImpersonationHandler = new ImpersonateUser(); var domainUser = UserName.Split('\\'); var userPart = UserName; var domainPart = ""; if (domainUser.Length == 2) { userPart = domainUser[1]; domainPart = domainUser[0]; } ImpersonationHandler.Impersonate(domainPart, userPart, Password); }