protected override bool RunDialog(IntPtr hwndOwner) { if (Environment.OSVersion.Version.Major < 5) { throw new PlatformNotSupportedException("The Credential Management API requires Windows XP / Windows Server 2003 or later."); } Win32Native.CredUIInfo credInfo = new Win32Native.CredUIInfo(hwndOwner, this.caption, this.message, this.banner); StringBuilder usr = new StringBuilder(Win32Native.CREDUI_MAX_USERNAME_LENGTH); StringBuilder pwd = new StringBuilder(Win32Native.CREDUI_MAX_PASSWORD_LENGTH); if (!string.IsNullOrEmpty(this.User)) { if (!string.IsNullOrEmpty(this.Domain)) { usr.Append(this.Domain + "\\"); } usr.Append(this.User); } if (this.Password != null) { pwd.Append(this.PasswordToString()); } try { Win32Native.CredUIReturnCodes result = Win32Native.CredUIPromptForCredentials( ref credInfo, this.target, IntPtr.Zero, 0, usr, Win32Native.CREDUI_MAX_USERNAME_LENGTH, pwd, Win32Native.CREDUI_MAX_PASSWORD_LENGTH, ref this.saveChecked, this.flags); switch (result) { case Win32Native.CredUIReturnCodes.NO_ERROR: LoadUserDomainValues(usr); LoadPasswordValue(pwd); return(true); case Win32Native.CredUIReturnCodes.ERROR_CANCELLED: this.User = null; this.Password = null; return(false); default: throw new InvalidOperationException(TranslateReturnCode(result)); } } finally { usr.Remove(0, usr.Length); pwd.Remove(0, pwd.Length); if (this.banner != null) { Win32Native.DeleteObject(credInfo.hbmBanner); } } }
/// <summary> /// Do Not Use. /// </summary> /// <param name="hwndOwner">handle</param> /// <returns>bool</returns> protected override bool RunDialog(IntPtr hwndOwner) { Win32Native.CredUIInfo credInfo = new Win32Native.CredUIInfo(hwndOwner, this.caption, this.message, null); StringBuilder usr = new StringBuilder(Win32Native.CREDUI_MAX_USERNAME_LENGTH); StringBuilder pwd = new StringBuilder(Win32Native.CREDUI_MAX_PASSWORD_LENGTH); if (!string.IsNullOrEmpty(this.User)) { usr.Append(this.User); } if (this.Password != null) { pwd.Append(this.PasswordAsString); } else { if (this.passwordIn != null) { pwd.Append(this.passwordIn); } } try { Win32Native.CredUIReturnCodes result = Win32Native.CredUIPromptForCredentials( ref credInfo, this.target, IntPtr.Zero, 0, usr, Win32Native.CREDUI_MAX_USERNAME_LENGTH, pwd, Win32Native.CREDUI_MAX_PASSWORD_LENGTH, ref this.saveChecked, this.flags); switch (result) { case Win32Native.CredUIReturnCodes.NO_ERROR: LoadUserDomainValues(usr); LoadPasswordValue(pwd); return(true); case Win32Native.CredUIReturnCodes.ERROR_CANCELLED: this.User = null; this.Password = null; return(false); default: throw new InvalidOperationException(TranslateReturnCode(result)); } } finally { usr.Remove(0, usr.Length); pwd.Remove(0, pwd.Length); } }
protected override bool RunDialog(IntPtr hwndOwner) { if (Environment.OSVersion.Version.Major < 5) { throw new PlatformNotSupportedException("The Credential Management API requires Windows XP / Windows Server 2003 or later."); } var credInfo = new Win32Native.CredUIInfo(hwndOwner, _caption, _message, _banner); var usr = new StringBuilder(Win32Native.CREDUI_MAX_USERNAME_LENGTH); var pwd = new StringBuilder(Win32Native.CREDUI_MAX_PASSWORD_LENGTH); if (!string.IsNullOrEmpty(User)) { if (!string.IsNullOrEmpty(Domain)) { usr.Append(Domain + "\\"); } usr.Append(User); } if (Password != null) { pwd.Append(PasswordToString()); } try { var result = Win32Native.CredUIPromptForCredentials( ref credInfo, _target, IntPtr.Zero, 0, usr, Win32Native.CREDUI_MAX_USERNAME_LENGTH, pwd, Win32Native.CREDUI_MAX_PASSWORD_LENGTH, ref _saveChecked, _flags); switch (result) { case Win32Native.CredUIReturnCodes.NO_ERROR: LoadUserDomainValues(usr); LoadPasswordValue(pwd); return true; case Win32Native.CredUIReturnCodes.ERROR_CANCELLED: User = null; Password = null; return false; default: throw new InvalidOperationException(TranslateReturnCode(result)); } } finally { usr.Remove(0, usr.Length); pwd.Remove(0, pwd.Length); if (_banner != null) { Win32Native.DeleteObject(credInfo.hbmBanner); } } }