ASP.NET 2.0 Unleashed
Using the ChangePassword Control
The ChangePassword control enables a user (or administrator) to change a user password. The page in Listing 20.29 illustrates how you can use this control. Listing 20.29. ShowChangePassword.aspx
The form in Listing 20.29 includes form fields for entering your username, old password, and new password (see Figure 20.13). After you submit the form, your old password is changed to the new password. Figure 20.13. Changing your password with the ChangePassword control.
Notice that the ChangePassword control in Listing 20.29 includes a DisplayUserName property. When this property is enabled, the username form field is rendered. You don't need to include the DisplayUserName property when you place the page within a password-protected section of your web application. In that case, the ChangePassword control uses the name of the current user automatically. Sending a Change Password Email
After the user changes his password, you can use the ChangePassword control to automatically send an email message that contains the new password. The page in Listing 20.30 contains a ChangePassword control that automatically sends an email. Note You can send a user's password in an email message even when password is encrypted or hashed by the membership provider.
Listing 20.30. ChangePasswordEmail.aspx
Notice that the ChangePassword control in Listing 20.30 includes a MailDefinition property that defines the email sent by the control. The ChangePassword control emails the message contained in Listing 20.31. Listing 20.31. ChangePassword.txt
The email message in Listing 20.31 includes two special expressions: <% UserName %> and <% Password %>. When the email is sent, the user's existing username and new password are substituted for these expressions. Note The MailDefinition class uses the email server configured by the smtp element in the web configuration file. For more information on configuring the smtp element, see the earlier section of this chapter, "Sending a Create User Email Message." Using Templates with the ChangePassword Control
If you need to completely modify the appearance of the ChangePassword control, then you can use templates to format the control. The ChangePassword control supports both a ChangePasswordTemplate and a SuccessTemplate. The page in Listing 20.32 illustrates how you can use both the templates supported by the ChangePassword control (see Figure 20.14). Figure 20.14. Customizing the ChangePassword control with templates.
Listing 20.32. ChangePasswordTemplate.aspx
You can use controls with the following IDs in the ChangePasswordTemplate template:
You also can add Button controls with the following values for the CommandName property:
|
Категории