1. Trang chủ
  2. » Công Nghệ Thông Tin

ASP.NET 4 Unleased - p 122 pot

10 86 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 622,55 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

The page in Listing 26.29 contains both a LoginName and LoginStatus control.. ptg CHAPTER 26 Using the Login Controls FIGURE 26.12 Displaying the current username with the LoginName con

Trang 1

CHAPTER 26 Using the Login Controls

LISTING 26.28 ShowLoginStatus.aspx

<%@ Page Language=”C#” %>

<!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 id=”Head1” runat=”server”>

<title>Show LoginStatus</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:LoginStatus

id=”LoginStatus1”

Runat=”server” />

<hr />

Additional information is available to registered users Log in to view

the additional information

</div>

</form>

</body>

</html>

After you open the page in Listing 26.28, if you click the Login link, you are redirected to

the Login page If you enter a valid username and password, you are redirected back to the

ShowLoginStatus.aspx page

The LoginStatus control supports the following properties:

LoginImageUrl—Enables you to specify an image for the Login link

LoginText—Enables you to specify the text for the Login link

LogoutAction—Enables you to control what happens when the Logout link is

clicked Possible values are Redirect, RedirectToLoginPage, and Refresh

LogoutImageUrl—Enables you to specify an image for the Logout link

LogoutPageUrl—Enables you to specify a page to which the user is redirected when

the user logs out This property is ignored unless the LogoutAction property is set to

the value Redirect

LogoutText—Enables you to specify the text for the Logout link

Trang 2

The LoginStatus control also supports the following two events:

LoggingOut—Raised before the user is logged out

LoggedOut—Raised after the user is logged out

Using the LoginName Control

The LoginName control displays the current user’s registered username If the current user

is not authenticated, the LoginName control renders nothing

The page in Listing 26.29 contains both a LoginName and LoginStatus control

LISTING 26.29 ShowLoginName.aspx

<%@ Page Language=”C#” %>

<!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 id=”Head1” runat=”server”>

<title>Show LoginName</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:LoginName

id=”LoginName1”

FormatString=”{0} /”

Runat=”server” />

<asp:LoginStatus

id=”LoginStatus1”

Runat=”server” />

<hr />

Additional information is available to registered users Log in to view

the additional information

</div>

</form>

</body>

</html>

Trang 3

ptg CHAPTER 26 Using the Login Controls

FIGURE 26.12 Displaying the current username with the LoginName control

When you first open the page in Listing 26.29, the LoginName control displays nothing

However, if you login by clicking the Login link, the LoginName control displays your

user-name (see Figure 26.12)

The LoginName control supports the following property:

FormatString—Enables you to format the username when the username is rendered

Using the ChangePassword Control

The ChangePassword control enables a user (or administrator) to change a user password

The page in Listing 26.30 illustrates how you can use this control

LISTING 26.30 ShowChangePassword.aspx

<%@ Page Language=”C#” %>

<!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 id=”Head1” runat=”server”>

<style type=”text/css”>

.changePassword

Trang 4

{

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 id=”form1” runat=”server”>

<div>

<asp:LoginName ID=”LoginName1” runat=”server” />

<asp:ChangePassword

id=”ChangePassword1”

InstructionText=”Complete this form to create

a new password.”

DisplayUserName=”true”

ContinueDestinationPageUrl=”~/Default.aspx”

CancelDestinationPageUrl=”~/Default.aspx”

CssClass=”changePassword”

TitleTextStyle-CssClass=”changePassword_title”

InstructionTextStyle-CssClass=”changePassword_instructions”

ChangePasswordButtonStyle-CssClass=”changePassword_button”

CancelButtonStyle-CssClass=”changePassword_button”

ContinueButtonStyle-CssClass=”changePassword_button”

Runat=”server” />

Trang 5

ptg CHAPTER 26 Using the Login Controls

FIGURE 26.13 Changing your password with the ChangePassword control

</div>

</form>

</body>

</html>

The form in Listing 26.30 includes form fields for entering your username, old password,

and new password (see Figure 26.13) After you submit the form, your old password is

changed to the new password

The ChangePassword control in Listing 26.30 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

automati-cally send an email message that contains the new password The page in Listing 26.31

contains a ChangePassword control that automatically sends an email

Trang 6

NOTE

You can send a user’s password in an email message even when the password is

encrypted or hashed by the membership provider

LISTING 26.31 ChangePasswordEmail.aspx

<%@ Page Language=”C#” %>

<!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 id=”Head1” runat=”server”>

<title>ChangePassword Email</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:ChangePassword

id=”ChangePassword1”

DisplayUserName=”true”

Runat=”server”>

<MailDefinition

From=”Admin@YourSite.com”

BodyFileName=”ChangePassword.txt”

Subject=”Your New Password” />

</asp:ChangePassword>

</div>

</form>

</body>

</html>

The ChangePassword control in Listing 26.31 includes a MailDefinition property that

defines the email sent by the control The ChangePassword control emails the message

contained in Listing 26.32

LISTING 26.32 ChangePassword.txt

<%UserName%>,

your new password is <%Password%>

Trang 7

ptg CHAPTER 26 Using the Login Controls

FIGURE 26.14 Customizing the ChangePassword control with templates

The email message in Listing 26.32 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, you can

use templates to format the control The ChangePassword control supports both a

ChangePasswordTemplate and a SuccessTemplate

The page in Listing 26.33 illustrates how you can use both the templates supported by the

ChangePassword control (see Figure 26.14)

Trang 8

LISTING 26.33 ChangePasswordTemplate.aspx

<%@ Page Language=”C#” %>

<!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 id=”Head1” runat=”server”>

<title>ChangePassword Template</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:ChangePassword

id=”ChangePassword1”

DisplayUserName=”true”

Runat=”server”>

<ChangePasswordTemplate>

<h1>Change Password</h1>

<asp:Label

id=”FailureText”

EnableViewState=”false”

ForeColor=”Red”

Runat=”server” />

<br />

<asp:Label

id=”lblUserName”

Text=”User Name:”

AssociatedControlID=”UserName”

Runat=”server” />

<br />

<asp:TextBox

id=”UserName”

Runat=”server” />

<br /><br />

<asp:Label

id=”lblCurrentPassword”

Text=”Current Password:”

AssociatedControlID=”CurrentPassword”

Runat=”server” />

<br />

<asp:TextBox

id=”CurrentPassword”

TextMode=”Password”

Runat=”server” />

<br /><br />

Trang 9

CHAPTER 26 Using the Login Controls

<asp:Label

id=”lblNewPassword”

Text=”New Password:”

AssociatedControlID=”NewPassword”

Runat=”server” />

<br />

<asp:TextBox

id=”NewPassword”

TextMode=”Password”

Runat=”server” />

<br /><br />

<asp:Button

id=”btnChangePassword”

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

Trang 10

Using the PasswordRecovery Control

If a user forgets her password, 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 26.34 contains a PasswordRecovery control

LISTING 26.34 ShowPasswordRecovery.aspx

<%@ Page Language=”C#” %>

<!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 id=”Head1” runat=”server”>

<style type=”text/css”>

.passwordRecovery

{

font:14px Verdana,Sans-Serif;

background-color:lightblue;

border:solid 3px black;

padding:4px;

}

.passwordRecovery_title

{

background-color:darkblue;

color:white;

font-weight:bold;

}

.passwordRecovery_instructions

{

font-size:12px;

text-align:left;

padding:10px;

}

.passwordRecovery_button

{

border:solid 1px black;

padding:3px;

}

</style>

<title>Show PasswordRecovery</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

Ngày đăng: 06/07/2014, 18:20

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN