Using the PasswordRecovery ControlIf a user forgets her password, then she can use the PasswordRecovery control to email herself her password. The PasswordRecovery control either sends the user's original password or resets the password and sends the new password. The page in Listing 20.33 contains a PasswordRecovery control. Listing 20.33. ShowPasswordRecovery.aspx
After you open the page in Listing 20.33 in your web browser, you are first asked to enter your username (see Figure 20.15). Next, you are asked to enter the answer to the security question that you entered when registering. Finally, a password is emailed to your registered email account. Figure 20.15. Retrieving a lost password with the PasswordRecovery control.Note Before you use the PasswordRecovery control, you must specify your mail server settings in your application's web configuration file. See the earlier section in this chapter, "Sending a Create User Email Message." By default, the PasswordRecovery control first resets your password before sending you the password. In the next section, you learn how to send a user's original password. Sending the Original PasswordBy default, the PasswordRecovery control does not send a user's original password. If you don't want the PasswordRecovery control to reset a user's password before sending it, then you must change the configuration of the membership provider. Three configuration settings matter: passwordFormat, enablePasswordRetrieval, and enablePasswordReset. By default, the passwordFormat attribute has the value Hashed. When passwords are hashed, the PasswordRecovery control cannot send a user's original password. This limitation makes sense because when passwords are hashed, the actual passwords are never stored anywhere. If you want to send a user his original password, then you need to set the passwordFormat attribute to either the value Clear or Encrypted. By default, the enablePasswordRetrieval attribute has the value False. Therefore, if you want to send a user his original password, you must enable this property in the web configuration file. Finally, by default, the enablePasswordReset attribute has the value true. Regardless of the value of the passwordFormat or enablePasswordRetrieval attributes, you can always reset a user's password and email the new password to the user. The web configuration file in Listing 20.34 contains the necessary configuration settings to enable a user's original password to be sent. Listing 20.34. Web.Config
The configuration file in Listing 20.34 causes passwords to be stored in plain text rather than hashed. Furthermore, password retrieval is enabled. Requiring a Security Question and AnswerWhen you use the CreateUserWizard control to register, you are required to select a security question and answer. The PasswordRecovery control displays a form that contains the security question. If you cannot enter the correct security answer, then your password is not sent. If you do not want to require users to answer a security question before receiving their passwords, then you can modify the configuration of the membership provider. The web configuration file in Listing 20.35 assigns the value false to the requiresQuestionAndAnswer attribute. Listing 20.35. Web.Config
Using Templates with the PasswordRecovery ControlIf you need to completely customize the appearance of the PasswordRecovery control, you can use templates. The PasswordRecovery control supports the following three types of templates:
The page in Listing 20.36 illustrates how you can use all three of these templates. Listing 20.36. PasswordRecoveryTemplate.aspx
The UserNameTemplate must contain a control with an ID of UserName. You also can include a control with an ID of FailureText when you want to display error messages. This template also must contain a Button control with a CommandName that has the value Submit. The QuestionTemplate must contain a control with an ID of Question and a control with an ID of Answer. Optionally, you can include a FailureText control when you want to display error messages. It also must have a Button control with a CommandName that has the value Submit. The SuccessTemplate, on the other hand, does not require any special controls. Notice that the PasswordRecovery control in Listing 20.36 includes a MailDefinition property that references a custom email message. The message is contained in Listing 20.37. Listing 20.37. PasswordRecovery.txt
The email message in Listing 20.37 contains substitution expressions for both the username and password. |