2.4 How to use the ASP.NET ChangePassword control

ASP.NET 2.0’s new ChangePassword control gives you the ability to automate the process of a user wanting to change their password. You can configure the new ChangePassword control to accepts the username and the password the user wishes to change. You can also configure it such that the user cannot enter his username, but should be logged into the application as a user to change the password.The new ChangePassword control also can be configured such that after changing the password, the new password can be e-mailed back to the user.

The ChangePassword control in its simplest form will look like the following:

<asp:ChangePassword ID="ChangePassword1" runat="server">
    </asp:ChangePassword>

The ChangePassword control can send a new password e-mail to the user using the <MailDefinition> child element. The following is a sample code of how this will look like:

    <asp:ChangePassword id="ChangePassword1" runat="Server" >
        <MailDefinition
            From="name@domain.com"
            Subject="Subject Line"
            BodyFileName=”BodyFile.txt">
        </MailDefinition>
    </asp:ChangePassword>

Note:

  • The body of the e-mail message will be taken from the file "BodyFile.txt " in the BodyFileName attribute.
  • This .txt file can include <%UserName%> and <%Password%> so you can put the user’s account name and password into the email.

In ASP.NET 2.0, you have the ability to customize your ChangePassword control by using any of the optional attributes listed below.

Attribute

Description

id

The ID for the ChangePassword control

runat

runat="Server" is needed for all ASP.NET server controls.

CancelButtonImageUrl

The URL for the image used for the Cancel button.

CancelButtonText

The text for the Cancel button.

CancelButtonType

The Cancel button type can be Button, Link, or Image.

CancelDestinationPageUrl

The URL of the page after the Cancel button is clicked.

ChangePasswordButtonImageUrl

The URL for the Change Password button image on the Success page.

ChangePasswordButtonText

The ChangePassword button text.

ChangePasswordButtonType

The ChangePassword button type can be Button,“Link, or Image.

ChangePasswordFailureText

The text if the password change is failed.

ChangePasswordTitleText

The text for the ChangePassword title area.

ConfirmNewPasswordLabelText

The text for the ConfirmPassword label field.

ContinueButtonImageUrl

The URL for the Continue button image.

ContinueButtonText

The Continue button text.

ContinueButtonType

The Continue button type can be Button, Link, or Image.

ContinueDestinationPageUrl

The URLof page the user is taken to after clicking the Continue button.

CreateUserText

The text for the CreateUser link.

CreateUserUrl

The URL for theCreateUser page.

DisplayUserName

This is a Boolean check box, to indicate if you want the user to enter the username when changing the password or the user to be loggied in to the system. If True, the ChangePassword control can also be used to change the password of an account other than the one to which the user is currently logged in, if False they can only change the password of the logged in account.

InstructionText

The text for the instructions to use the ChangePassword control.

NewPasswordLabelText

The text for the New Password label field.

NewPasswordRegularExpression

The regular expression which is used as a validation for the new password.

PasswordHintText

The text diplyed for the instruction for the new password and its requiermetns, like how many character, minimum length, etc…

PasswordLabelText

The text displayed for the Current Password label field.

PasswordRecoveryText

The text for the Password Recovery page link

PasswordRecoveryUrl

The URL for the Password Recovery page.

SuccessPageUrl

The URL for theSuccess page if the password has been changed.

SuccessText

The text for the message if the password change is successful, and this is not displayed if the SuccessPageUrl is provided.

UserNameLabelText

The text for the Username label field.

Attributes for the “ChangePassword” control


The following is the code for a customized ChangePassword control.

    <asp:ChangePassword id="ChangePassword1" runat="Server"
        ChangePasswordTitleText="Change Your Password<br />"
        PasswordLabelText="Enter your current password:"
        NewPasswordLabelText="Enter the new password:"
        ConfirmNewPasswordLabelText="Confirm the new password:">
    </asp:ChangePassword>

Note:

  • The ChangePassword control requires the user to be logged in to change the password by default. You can change this by specifying the DiplayeUsername=True attribute. This will display a Useraname "textbox, where the user has to enter the valid user name and password to change the password for any user.
  • The ChangePassword control has two views, and they are:
    • The Initial view – this is the Change Password view including the text boxes for the user to enter the new password.
    • The Success view – displayed when the password change is successful with a confirmation message. The success view is not displayed if the SuccessPageUrl is provided, but instead the page in the URL specified will be shown.

Table of Contents