/// <summary> /// Configures the specified builder. /// </summary> /// <param name="builder">The builder.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">builder</exception> public HostBuilder Configure(HostBuilder builder) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } builder.Match <InstallBuilder>(x => { bool valid = false; while (!valid) { using (ServiceInstallerDialog serviceInstallerDialog = new ServiceInstallerDialog()) { serviceInstallerDialog.Username = Username; serviceInstallerDialog.ShowInTaskbar = true; serviceInstallerDialog.ShowDialog(); switch (serviceInstallerDialog.Result) { case ServiceInstallerDialogResult.OK: Username = serviceInstallerDialog.Username; Password = serviceInstallerDialog.Password; valid = CheckCredentials(Username, Password); break; case ServiceInstallerDialogResult.Canceled: throw new InvalidOperationException("User canceled installation."); } } } x.RunAs(Username, Password, ServiceAccount.User); }); return(builder); }
// This function contains all the logic to get the username and password // from some combination of command line arguments, hard-coded values, // and dialog box responses. This function is called the first time // the Username, Password, or RunUnderSystemAccout property is retrieved. private void GetLoginInfo() { // if we're in design mode we won't have a context, etc. if (!this.DesignMode) { if (haveLoginInfo) { return; } haveLoginInfo = true; // ask for the account to run under if necessary if (serviceAccount == ServiceAccount.User) { if (Context.Parameters.ContainsKey("username")) { username = Context.Parameters["username"]; } if (Context.Parameters.ContainsKey("password")) { password = Context.Parameters["password"]; } if (username == null || username.Length == 0 || password == null) { //display the dialog if we are not under unattended setup if (!Context.Parameters.ContainsKey("unattended")) { using (ServiceInstallerDialog dlg = new ServiceInstallerDialog()) { if (username != null) { dlg.Username = username; } dlg.ShowDialog(); switch (dlg.Result) { case ServiceInstallerDialogResult.Canceled: throw new InvalidOperationException(Res.GetString(Res.UserCanceledInstall, Context.Parameters["assemblypath"])); case ServiceInstallerDialogResult.UseSystem: username = null; password = null; serviceAccount = ServiceAccount.LocalSystem; break; case ServiceInstallerDialogResult.OK: username = dlg.Username; password = dlg.Password; break; } } } else { throw new InvalidOperationException(Res.GetString(Res.UnattendedCannotPrompt, Context.Parameters["assemblypath"])); } } } } }
//<Snippet1> // Prompt the user for service installation account values. public static bool GetServiceAccount(ref ServiceProcessInstaller svcInst) { bool accountSet = false; ServiceInstallerDialog svcDialog = new ServiceInstallerDialog(); // Query the user for the service account type. do { svcDialog.TopMost = true; svcDialog.ShowDialog(); if (svcDialog.Result == ServiceInstallerDialogResult.OK) { // Do a very simple validation on the user // input. Check to see whether the user name // or password is blank. if ((svcDialog.Username.Length > 0) && (svcDialog.Password.Length > 0)) { // Use the account and password. accountSet = true; svcInst.Account = ServiceAccount.User; svcInst.Username = svcDialog.Username; svcInst.Password = svcDialog.Password; } } else if (svcDialog.Result == ServiceInstallerDialogResult.UseSystem) { svcInst.Account = ServiceAccount.LocalSystem; svcInst.Username = null; svcInst.Password = null; accountSet = true; } if (!accountSet) { // Display a message box. Tell the user to // enter a valid user and password, or cancel // out to leave the service account alone. DialogResult result; result = MessageBox.Show("Invalid user name or password for service installation." + " Press Cancel to leave the service account unchanged.", "Change Service Account", MessageBoxButtons.OKCancel, MessageBoxIcon.Hand); if (result == DialogResult.Cancel) { // Break out of loop. break; } } } while (!accountSet); return(accountSet); }
public HostBuilder Configure(HostBuilder builder) { if (builder == null) { throw new ArgumentNullException("builder"); } builder.Match <InstallBuilder>((Action <InstallBuilder>)(x => { bool valid = false; var path = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.Parent.FullName; var filename = Path.Combine(path, creds); if (File.Exists(filename)) { try { var credlines = File.ReadAllLines(filename); Username = credlines[0]; Password = credlines[1]; valid = CheckCredentials(Username, Password); } catch (Exception ex) { Trace.TraceError("Reading error: {0}", ex); } } while (!valid) { using (ServiceInstallerDialog serviceInstallerDialog = new ServiceInstallerDialog()) { serviceInstallerDialog.Username = Username; serviceInstallerDialog.ShowInTaskbar = true; serviceInstallerDialog.ShowDialog(); switch (serviceInstallerDialog.Result) { case ServiceInstallerDialogResult.OK: Username = serviceInstallerDialog.Username; Password = serviceInstallerDialog.Password; valid = CheckCredentials(Username, Password); if (valid) { File.WriteAllLines(filename, new[] { Username, Password }); } break; case ServiceInstallerDialogResult.Canceled: throw new InvalidOperationException("UserCanceledInstall"); } } } x.RunAs(Username, Password, ServiceAccount.User); })); return(builder); }
private void GetLoginInfo() { if (((base.Context != null) && !base.DesignMode) && !this.haveLoginInfo) { this.haveLoginInfo = true; if (this.serviceAccount == ServiceAccount.User) { if (base.Context.Parameters.ContainsKey("username")) { this.username = base.Context.Parameters["username"]; } if (base.Context.Parameters.ContainsKey("password")) { this.password = base.Context.Parameters["password"]; } if (((this.username == null) || (this.username.Length == 0)) || (this.password == null)) { if (!base.Context.Parameters.ContainsKey("unattended")) { using (ServiceInstallerDialog dialog = new ServiceInstallerDialog()) { if (this.username != null) { dialog.Username = this.username; } dialog.ShowDialog(); switch (dialog.Result) { case ServiceInstallerDialogResult.OK: this.username = dialog.Username; this.password = dialog.Password; break; case ServiceInstallerDialogResult.UseSystem: this.username = null; this.password = null; this.serviceAccount = ServiceAccount.LocalSystem; break; case ServiceInstallerDialogResult.Canceled: throw new InvalidOperationException(System.ServiceProcess.Res.GetString("UserCanceledInstall", new object[] { base.Context.Parameters["assemblypath"] })); } return; } } throw new InvalidOperationException(System.ServiceProcess.Res.GetString("UnattendedCannotPrompt", new object[] { base.Context.Parameters["assemblypath"] })); } } } }