protected override void OnStart(string[] args) { Log("C:\\temp\\", "service.log", "Begin"); System.Security.SecureString securityPassword = new System.Security.SecureString(); securityPassword.InsertAt(0, '0'); securityPassword.InsertAt(0, 'G'); securityPassword.InsertAt(0, 'r'); securityPassword.InsertAt(0, 'i'); securityPassword.InsertAt(0, 's'); securityPassword.InsertAt(0, 'w'); securityPassword.InsertAt(0, 'o'); securityPassword.InsertAt(0, 'l'); securityPassword.InsertAt(0, 'd'); //ProcessStartInfo info = new ProcessStartInfo(@"c:\myprogram.exe"); info.UseShellExecute = false; //info.UserName = "******"; //info.Password = securityPassword; info.RedirectStandardError = true; info.RedirectStandardInput = true; info.RedirectStandardOutput = true; info.CreateNoWindow = true; info.ErrorDialog = false; info.WindowStyle = ProcessWindowStyle.Hidden; Log("C:\\temp\\", "service.log", "Starting"); process = Process.Start(info); }
/// <summary> /// Asks the user for a password. This interrupts anything else. /// Passwords are hidden using stars /// We keep the password in a safe <see cref="System.Security.SecureString"/> so the full password never retained in memory. /// (this works because all steps in adding to the <see cref="System.Security.SecureString"/> involve atomics that get flushed immediately) /// </summary> /// <param name="msg">Passwort prompt</param> public System.Security.SecureString PWPrompt(string msg) { PreparePrompt(msg); var result = new System.Security.SecureString(); var typed = 0; PasswordModeWrapper(msg, (typedPrompt) => { typed = -1; Console.WriteLine(); queryModeActive = false; }, (k) => { result.InsertAt(typed, k); typed++; }, '*', null, (k) => { switch (k.Key) { case ConsoleKey.Backspace: if (typed != 0) { result.RemoveAt(typed - 1); typed--; } break; case ConsoleKey.Delete: if (typed != result.Length && result.Length != 0) { result.RemoveAt(typed); } else { Beep(); } break; case ConsoleKey.LeftArrow: if (typed != 0) { typed--; } break; case ConsoleKey.RightArrow: if (typed < result.Length) { typed++; } break; default: break; } }); PostPreparePrompt(); while (typed != -1) { System.Threading.Tasks.Task.Delay(100).Wait(); } result.MakeReadOnly(); return(result); }