/// <summary> /// When overridden in a derived class, specifies a common dialog box. /// </summary> /// <param name="hwndOwner">A value that represents the window handle of the owner window for the common dialog box.</param> /// <returns> /// true if the dialog box was successfully run; otherwise, false. /// </returns> protected override bool RunDialog(IntPtr hwndOwner) { if (Environment.OSVersion.Version.Major < 5) { throw new PlatformNotSupportedException(Properties.Resources.FunctionNotSupported); } UnsafeNativeMethods.CredUIInfo credInfo = new UnsafeNativeMethods.CredUIInfo(hwndOwner, this.caption, this.message, this.banner); StringBuilder usr = new StringBuilder(UnsafeNativeMethods.CREDUI_MAX_USERNAME_LENGTH); StringBuilder pwd = new StringBuilder(UnsafeNativeMethods.CREDUI_MAX_PASSWORD_LENGTH); if (this.credentials != null) { usr.Append(this.credentials.PrincipalName); pwd.Append(this.credentials.PasswordToString()); } try { //For more info see: //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthn/security/creduipromptforcredentials.asp //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/dpapiusercredentials.asp?frame=true UnsafeNativeMethods.CredUIReturnCodes result = UnsafeNativeMethods.CredUIPromptForCredentials( ref credInfo, this.target, IntPtr.Zero, 0, usr, UnsafeNativeMethods.CREDUI_MAX_USERNAME_LENGTH, pwd, UnsafeNativeMethods.CREDUI_MAX_PASSWORD_LENGTH, ref this.saveChecked, this.flags); switch (result) { case UnsafeNativeMethods.CredUIReturnCodes.NO_ERROR: this.Credentials = new SecureCredential(usr.ToString(), pwd); return(true); case UnsafeNativeMethods.CredUIReturnCodes.ERROR_CANCELLED: this.Credentials = null; return(false); default: throw new InvalidOperationException(TranslateReturnCode(result)); } } finally { usr.Remove(0, usr.Length); pwd.Remove(0, pwd.Length); if (this.banner != null) { UnsafeNativeMethods.DeleteObject(credInfo.hbmBanner); } } }
/// <summary> /// The ConfirmCredentials method is called after ShowDialog, /// to confirm the validity of the credential harvested. /// </summary> /// <remarks> /// After calling ShowDialog and before calling <see cref="ConfirmCredentials"/>, /// the caller must determine whether or not the credentials are actually valid by /// using the credentials to access the resource specified by targetName. /// The results of that validation test are passed to <see cref="ConfirmCredentials"/> in the /// confirm parameter. /// </remarks> /// <param name="confirm">Specifies whether the credentials returned from the prompt function are valid. /// If TRUE, the credentials are stored in the credential manager. /// If FALSE, the credentials are not stored and various pieces of memory are cleaned up. /// </param> /// <permission cref="UIPermission">Demand for <see cref="UIPermissionWindow.SafeSubWindows"/> permission.</permission> public void ConfirmCredentials(bool confirm) { new UIPermission(UIPermissionWindow.SafeSubWindows).Demand(); UnsafeNativeMethods.CredUIReturnCodes result = UnsafeNativeMethods.CredUIConfirmCredentialsW(this.target, confirm); if (result != UnsafeNativeMethods.CredUIReturnCodes.NO_ERROR && result != UnsafeNativeMethods.CredUIReturnCodes.ERROR_NOT_FOUND && result != UnsafeNativeMethods.CredUIReturnCodes.ERROR_INVALID_PARAMETER) { throw new InvalidOperationException(TranslateReturnCode(result)); //Trace.TraceError(TranslateReturnCode(result)); } }
private void LoadUserDomainValues() { StringBuilder user = new StringBuilder(UnsafeNativeMethods.CREDUI_MAX_USERNAME_LENGTH); StringBuilder domain = new StringBuilder(UnsafeNativeMethods.CREDUI_MAX_DOMAIN_TARGET_LENGTH); UnsafeNativeMethods.CredUIReturnCodes result = UnsafeNativeMethods.CredUIParseUserNameW(this.principalName, user, UnsafeNativeMethods.CREDUI_MAX_USERNAME_LENGTH, domain, UnsafeNativeMethods.CREDUI_MAX_DOMAIN_TARGET_LENGTH); if (result == UnsafeNativeMethods.CredUIReturnCodes.NO_ERROR) { this.user = user.ToString(); this.domain = domain.ToString(); } else { this.user = this.principalName; this.domain = Environment.MachineName; } }
private static string TranslateReturnCode(UnsafeNativeMethods.CredUIReturnCodes result) { return(string.Format(CultureInfo.CurrentUICulture, Properties.Resources.CredUIReturn, result.ToString())); }