/// <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.); } }
/// <summary> /// Prompts the user for credentials with a specified prompt window /// caption, prompt message, user name, and target name. /// </summary> /// <param name="caption">The caption of the message window.</param> /// <param name="message">The text of the message.</param> /// <param name="userName">The user name whose credential is to be prompted for.</param> /// <param name="targetName">The name of the target for which the credential is collected.</param> /// <returns>Throws a NotImplementException exception.</returns> public override PSCredential PromptForCredential( string caption, string message, string userName, string targetName) { PromptCredentialsResult result = CredentialUI.PromptForCredentials(targetName, caption, message, userName, null); return(result == null ? null : new PSCredential(result.UserName, result.Password.ToSecureString())); }