Using the ChangePassword Control


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

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <style type="text/css">         .changePassword         {             font:14px Verdana,Sans-Serif;             background-color:lightblue;             border:solid 3px black;             padding:4px;         }         .changePassword_title         {             background-color:darkblue;             color:white;             font-weight:bold;         }         .changePassword_instructions         {             font-size:12px;             text-align:left;             padding:10px;         }         .changePassword_button         {             border:solid 1px black;             padding:3px;         }     </style>     <title>Show ChangePassword</title> </head> <body>     <form  runat="server">     <div>     <asp:LoginName  runat="server" />     <asp:ChangePassword                  InstructionText="Complete this form to create             a new password."         DisplayUserName="true"         ContinueDestinationPageUrl="~/Default.aspx"         CancelDestinationPageUrl="~/Default.aspx"         Css         TitleTextStyle-Css         InstructionTextStyle-Css         ChangePasswordButtonStyle-Css         CancelButtonStyle-Css         ContinueButtonStyle-Css         Runat="server" />     </div>     </form> </body> </html> 

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

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>ChangePassword Email</title> </head> <body>     <form  runat="server">     <div>     <asp:ChangePassword                  DisplayUserName="true"         Runat="server">         <MailDefinition             From="Admin@YourSite.com"             BodyFileName="ChangePassword.txt"             Subject="Your New Password" />     </asp:ChangePassword>     </div>     </form> </body> </html> 

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

<%UserName%>, your new password is <%Password%>. 

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

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>ChangePassword Template</title> </head> <body>     <form  runat="server">     <div>     <asp:ChangePassword                  DisplayUserName="true"         Runat="server">         <ChangePasswordTemplate>             <h1>Change Password</h1>             <asp:Label                                  EnableViewState="false"                 ForeColor="Red"                 Runat="server" />             <br />             <asp:Label                                  Text="User Name:"                 AssociatedControl                 Runat="server" />             <br />             <asp:TextBox                                  Runat="server" />             <br /><br />             <asp:Label                                  Text="Current Password:"                 AssociatedControl                 Runat="server" />             <br />             <asp:TextBox                                  TextMode="Password"                 Runat="server" />             <br /><br />             <asp:Label                                  Text="New Password:"                 AssociatedControl                 Runat="server" />             <br />             <asp:TextBox                                  TextMode="Password"                 Runat="server" />             <br /><br />             <asp:Button                                  Text="Change Password"                 CommandName="ChangePassword"                 Runat="server" />         </ChangePasswordTemplate>         <SuccessTemplate>             Your password has been changed!         </SuccessTemplate>     </asp:ChangePassword>     </div>     </form> </body> </html> 

You can use controls with the following IDs in the ChangePasswordTemplate template:

  • UserName

  • CurrentPassword

  • ConfirmPassword

  • NewPassword

  • FailureText

You also can add Button controls with the following values for the CommandName property:

  • ChangePassword

  • Cancel

  • Continue




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net