ValidatePasswordEventArgs Class (System.Web.Security)

Switch View :
ScriptFree
.NET Framework Class Library
ValidatePasswordEventArgs Class

Provides event data for the ValidatingPassword event of the MembershipProvider class.

Inheritance Hierarchy

System.Object
  System.EventArgs
    System.Web.Security.ValidatePasswordEventArgs

Namespace:  System.Web.Security
Assembly:  System.Web.ApplicationServices (in System.Web.ApplicationServices.dll)
Syntax

Visual Basic
Public NotInheritable Class ValidatePasswordEventArgs _
	Inherits EventArgs
C#
public sealed class ValidatePasswordEventArgs : EventArgs
Visual C++
public ref class ValidatePasswordEventArgs sealed : public EventArgs
F#
[<Sealed>]
type ValidatePasswordEventArgs =  
    class
        inherit EventArgs
    end

The ValidatePasswordEventArgs type exposes the following members.

Constructors

  Name Description
Public method ValidatePasswordEventArgs Creates a new instance of the ValidatePasswordEventArgs class.
Top
Properties

  Name Description
Public property Cancel Gets or sets a value that indicates whether the current create-user, change-password, or reset-password action will be canceled.
Public property FailureInformation Gets or sets an exception that describes the reason for the password-validation failure.
Public property IsNewUser Gets a value that indicates whether the ValidatingPassword event is being raised during a call to the CreateUser method.
Public property Password Gets the password for the current create-user, change-password, or reset-password action.
Public property UserName Gets the name of the membership user for the current create-user, change-password, or reset-password action.
Top
Methods

  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
Remarks

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

You can handle 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 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 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.

Examples

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

Visual Basic

Public Sub Page_Load()
    AddHandler Membership.ValidatingPassword, _
    New MembershipValidatePasswordEventHandler(AddressOf OnValidatePassword)
End Sub

Public Sub OnValidatePassword(sender As Object, _
                               args As ValidatePasswordEventArgs)
  Dim r As System.Text.RegularExpressions.Regex =  _
    New System.Text.RegularExpressions.Regex("(?=.{6,})(?=(.*\d){1,})(?=(.*\W){1,})")


  If Not r.IsMatch(args.Password) Then
    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
  End If
End Sub


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;
  }
}


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference

Other Resources