Edit

Share via


MembershipValidatePasswordEventHandler Delegate

Definition

Represents the method that will handle the ValidatingPassword event of the MembershipProvider class.

C#
public delegate void MembershipValidatePasswordEventHandler(object sender, ValidatePasswordEventArgs e);

Parameters

sender
Object

The MembershipProvider that raised the ValidatingPassword event.

e
ValidatePasswordEventArgs

A ValidatePasswordEventArgs object that contains the event data.

Examples

The following code example shows a ValidatingPassword event that validates the format of the password for the user, and then cancels the action if the password does not match the required format.

C#
public void Page_Load()
{
  Membership.ValidatingPassword +=
    new MembershipValidatePasswordEventHandler(OnValidatePassword);
}

public void OnValidatePassword(object sender,
                              ValidatePasswordEventArgs args)
{
  System.Text.RegularExpressions.Regex r =
    new System.Text.RegularExpressions.Regex(@"(?=.{6,})(?=(.*\d){1,})(?=(.*\W){1,})");


  if (!r.IsMatch(args.Password))
  {
    args.FailureInformation =
      new HttpException("Password must be at least 6 characters long and " +
                        "contain at least one number and one special character.");
    args.Cancel = true;
  }
}

Remarks

The MembershipValidatePasswordEventHandler delegate is defined for the ValidatingPassword event for a membership provider. The ValidatingPassword event is raised when the CreateUser method, the ChangePassword method, or the ResetPassword method of a membership provider is called.

You can use the ValidatingPassword event to validate password formats and values for membership users.

You can cancel the current CreateUser, ChangePassword, or ResetPassword action by setting the Cancel property of the supplied ValidatePasswordEventArgs to true during the ValidatingPassword event.

If you cancel the current action by setting the Cancel property to true, you can set the FailureInformation property of the supplied ValidatePasswordEventArgs to an exception that describes the reason for the password-validation failure. The calling method will throw the exception that the FailureInformation property is set to. If the FailureInformation property is null, the caller will throw a generic password-validation failure exception.

Extension Methods

GetMethodInfo(Delegate)

Gets an object that represents the method represented by the specified delegate.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also