private async Task ConnectImpl() { var promptForWindowsCredentials = CredentialUi.PromptForWindowsCredentials(SelectedLink.Title, "Please enter the password to launch " + SelectedLink.Title, SelectedLink.DefaultUserName, ""); if (promptForWindowsCredentials == null) { return; } try { Launcher.CreateProcess( promptForWindowsCredentials.UserName, promptForWindowsCredentials.DomainName, promptForWindowsCredentials.Password, SelectedLink.Path + " " + SelectedLink.Parameters); } catch (Exception ex) { var metroWindow = (MetroWindow)Application.Current.MainWindow; metroWindow.MetroDialogOptions.ColorScheme = MetroDialogColorScheme.Theme; var message = ex.Message; if (message == "A logon request contained an invalid logon type value") { message = "An invalid login type was detected. Make sure your username is a valid UPN (e.g. [email protected])"; } await metroWindow.ShowMessageAsync("Error launching application", message); } }
/// <summary> /// Calls the CredUICmdLinePromptForCredentials function that will either prompt for /// credentials or get the stored credentials for the specified target. /// </summary> /// <returns>The DialogResult that indicates the results of using CredUI.</returns> /// <remarks> /// Sets the user name, password and persistence state of the dialog. /// </remarks> internal DialogResult ShowPrompt() { /*StringBuilder locName = new StringBuilder(Credmgmtui.MaxUsernameLength); * StringBuilder pwd = new StringBuilder(Credmgmtui.MaxPasswordLength); * StringBuilder domain = new StringBuilder(100); * bool saveChecked = this.SaveChecked; * Credmgmtui.Flags flags = GetFlags();*/ lock (this) { // Call CredUi.dll to prompt for credentials. If the credentials for the target have already // been saved in the credential manager, then this method will return the credentials // and the UI will not be displayed. /*Credmgmtui.ReturnCodes code = Credmgmtui.CredUICmdLinePromptForCredentials( * this.Target, * IntPtr.Zero, 0, * locName, Credmgmtui.MaxUsernameLength, * pwd, Credmgmtui.MaxPasswordLength, * ref saveChecked, * flags * );*/ /*Credmgmtui.CREDUI_INFO credUi = new Credmgmtui.CREDUI_INFO * { * pszCaptionText = "Enter your credentials, please", * pszMessageText = "Enter your Exchange Online user principal name for the Exchange Web Services stored credential ([email protected])..." * }; * credUi.cbSize = Marshal.SizeOf(credUi);*/ /*uint authPackage = 0; * IntPtr outCredBuffer = new IntPtr(); * uint outCredSize;*/ /*Credmgmtui.ReturnCodes code = Credmgmtui.CredUIPromptForWindowsCredentials( * ref credUi, 1, ref authPackage, IntPtr.Zero, 0, out outCredBuffer, out outCredSize, ref saveChecked, flags);*/ PromptCredentialsResult res = CredentialUi.Prompt(LocalizibleStrings.EwsLogin, LocalizibleStrings.InputYourCred); if (res == null) { return(DialogResult.Cancel); } // Convert the password returned by the credential manager to a secure string. this.Password = ConvertToSecureString(new StringBuilder(res.Password)); // Get the user name stored in the credential manager. this.Name = res.UserName; // Get the value that indicates whether the credentials are persisted // in the credential manager. this.SaveChecked = res.IsSaveChecked; return(DialogResult.OK); //return GetCredPromptResult(res.); } }