Professional ASP.NET 3.5 in C# and Visual Basic Part 83 pot

10 193 0
Professional ASP.NET 3.5 in C# and Visual Basic Part 83 pot

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

Thông tin tài liệu

Evjen c16.tex V2 - 01/28/2008 2:51pm Page 777 Chapter 16: Membership and Role Management Listing 16-13: Providing a login for the end user using the Login control < %@ Page Language="VB" % > < html xmlns="http://www.w3.org/1999/xhtml" > < head runat="server" > < title > Login Page < /title > < /head > < body > < form id="form1" runat="server" > < asp:Login ID="Login1" Runat="server" > < /asp:Login > < /form > < /body > < /html > In the situation established here, if the unauthenticated user hits a different page in the application, he is redirected to the Login.aspx page. You can see how ASP.NET tracks the location in the URL from the address bar in the browser: http://localhost:18436/Membership/Login.aspx?ReturnUrl=%2fMembership%2fDefault.aspx The login page, using the Login control, is shown in Figure 16-7. Figure 16-7 From this figure, you can see that the Login control asks the user for a username and password. A check box allows a cookie to be stored on the client machine. This cookie enables the end user to bypass future logins. You can remove the check box and related text created to remember the user by setting the Login control’s DisplayRememberMe property to False . In addition to the DisplayRememberMe property, you can work with this aspect of the Login control by using the RememberMeText and the RememberMeSet properties. The RememberMeText property is pretty 777 Evjen c16.tex V2 - 01/28/2008 2:51pm Page 778 Chapter 16: Membership and Role Management self-explanatory because its value simply defines the text set next to the check box. The RememberMeSet property, however, is fairly interesting. The RememberMeSet property takes a Boolean value (by default, it is set to False ) that specifies whether to set a persistent cookie on the client’s machine after a user has logged in using the Login control. If set to True when the DisplayRememberMe property is also set to True , the check box is simply checked by default when the Login control is generated in the browser. If the DisplayRememberMe property is set to False (meaning the end user does not see the check box or cannot select the option of persisting the login cookie) and the RememberMeSet is set to True , a cookie is set on the user’s machine automatically without the user’s knowledge or choice in the matter. You should think carefully about taking this approach because end users sometimes use public computers, and this method would mean you are setting authorization cookies on public machines. This cookie remains on the client’s machine until the user logs out of the application (if this option is provided). With the persisted cookie, and assuming the end user has not logged out of the application, the user never needs to log in again when he returns to the application because his credentials are provided by the contents found in the cookie. After the end user has logged in to the application, he is returned to the page he originally intended to access. You can also modify the look-and-feel of the Login control just as you can for the other controls. One way to do this is by clicking the Auto Format link in the control’s smart tag. There you find a list of options for modifying the look-and-feel of the control (see Figure 16-8). Figure 16-8 Select the Colorful option, for example, and the code is modified. Listing 16-14 shows the code generated for this selection. Listing 16-14: A formatted Login control < asp:Login ID="Login1" Runat="server" BackColor="#FFFBD6" BorderColor="#FFDFAD" BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#333333" TextLayout="TextOnTop" > 778 Evjen c16.tex V2 - 01/28/2008 2:51pm Page 779 Chapter 16: Membership and Role Management < TextBoxStyle Font-Size="0.8em" / > < LoginButtonStyle BackColor="White" BorderColor="#CC9966" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#990000" / > < InstructionTextStyle Font-Italic="True" ForeColor="Black" / > < TitleTextStyle BackColor="#990000" Font-Bold="True" Font-Size="0.9em" ForeColor="White" / > < /asp:Login > From this listing, you can see that there are a number of subelements that are used to modify particular items displayed by the control. The available styling elements for the Login control include the following: ❑ < CheckboxStyle > ❑ < FailureTextStyle > ❑ < HyperLinkStyle > ❑ < InstructionTextStyle > ❑ < LabelStyle > ❑ < LoginButtonStyle > ❑ < TextBoxStyle > ❑ < TitleTextStyle > ❑ < ValidatorTextStyle > The Login control has numerous properties that allow you to alter how the control appears and behaves. An interesting change you can make is to add some links at the bottom of the control to provide access to additional resources. With these links, you can give users the capability to get help or register for the application so that they can be provided with any login credentials. You can provide links to do the following: ❑ Redirect users to a help page using the HelpPageText , HelpPageUrl ,and HelpPageIconUrl properties. ❑ Redirect users to a registration page using the CreateUserText , CreateUserUrl ,and Create UserIconUrl properties. ❑ Redirect users to a page that allows them to recover their forgotten passwords using the PasswordRecoveryText , PasswordRecoveryUrl ,and PasswordRecoveryIconUrl properties. When used, the Login control looks like what is shown in Figure 16-9. Logging In Users Programmatically Besides using the pre-built mechanics of the Login control, you can also perform this task programmati- cally using the Membership class. To validate credentials that you receive, you use the ValidateUser() method of this class. The ValidateUser() method takes a single signature: Membership.ValidateUser(username As String, password As String) This method is illustrated in Listing 16-15. 779 Evjen c16.tex V2 - 01/28/2008 2:51pm Page 780 Chapter 16: Membership and Role Management Figure 16-9 Listing 16-15: Validating a user’s credentials programmatically VB If Membership.ValidateUser(TextBox1.Text, TextBox2.Text) Then FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, False) Else Label1.Text = "You are not registered with the site." End If C# if (Membership.ValidateUser(TextBox1.Text, TextBox2.Text) { FormsAuthentication.RedirectFromLoginPage(TextBox1.Text.ToString(), false); } else { Label1.Text = "You are not registered with the site."; } The ValidateUser() method returns a Boolean value of True if the user credentials pass the test and False if they do not. From the code snippet in Listing 16-15, you can see that end users whose credentials are verified as correct are redirected from the login page using the RedirectFromLoginPage() method. This method takes the username and a Boolean value that specifies whether the credentials are persisted through a cookie setting. Locking Out Users Who Provide Bad Passwords When providing a user login form in any application you build, always guard against repeated bogus password attempts. If you have a malicious end user who knows a username, he may try to access the application by repeatedly trying different passwords. You want to guard against this kind of activity. You don’t want to allow this person to try hundreds of possible passwords with this username. 780 Evjen c16.tex V2 - 01/28/2008 2:51pm Page 781 Chapter 16: Membership and Role Management ASP.NET has built-in protection against this type of activity. If you look in the aspnet_Membership table, you see two columns focused on protecting against this. These columns are FailedPasswordAttempt- Count and FailedPasswordAttemptWindowStart . By default, a username can be used with an incorrect password in a login attempt only five times within a 10-minute window. On the fifth failed attempt, the account is locked down. This is done in ASP.NET by setting the IsLockedOut column to True . You can actually control the number of password attempts that are allowed and the length of the attempt window for your application. These two items are defined in the SqlMembershipProvider declaration in the machine.config file. You can change the values either in the server-wide configuration files or in your application’s web.config file. Changing these values in your web.config file is presented in Listing 16-16. Listing 16-16: Changing the values for password attempts in the provider declaration < configuration > < system.web > < membership defaultProvider="AspNetSqlMembershipProvider" > < providers > < clear / > < add connectionStringName="LocalSqlServer" applicationName="/" maxInvalidPasswordAttempts="3" passwordAttemptWindow="15" name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" / > < /providers > < /membership > < /system.web > < /configuration > To determine the number of password attempts that are allowed, use maxInvalidPasswordAttempts . This example changes the value to 3 , meaning that users are allowed to enter an incorrect password three times before being locked out (within the time window defined). The default value of the maxInvalid- PasswordAttempts attribute is 5 . You can set the time allowed for bad password attempts to 15 minutes using the passwordAttemptWindow attribute. The default value of this attribute is 10 ,soanextrafive minutes is added. Now that these items are in place, the next step is to test it. Listing 16-17 provides you with an example of the test. It assumes you have an application established with a user already in place. Listing 16-17: A sample page to test password attempts VB < %@ Page Language="VB" % > < script runat="server" > Protected Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Continued 781 Evjen c16.tex V2 - 01/28/2008 2:51pm Page 782 Chapter 16: Membership and Role Management If CheckBox1.Checked = True Then Dim user As MembershipUser = Membership.GetUser(TextBox1.Text) user.UnlockUser() End If If Membership.ValidateUser(TextBox1.Text, TextBox2.Text) Then Label1.Text = "You are logged on!" Else Dim user As MembershipUser = Membership.GetUser(TextBox1.Text) Label1.Text = "Locked out value: " & user.IsLockedOut.ToString() End If End Sub < /script > < html xmlns="http://www.w3.org/1999/xhtml" > < head runat="server" > < title > Login Page < /title > < /head > < body > < form id="form1" runat="server" > < div > < h1 > Login User < /h1 > < p > < asp:CheckBox ID="CheckBox1" runat="server" Text="Unlock User" / > < /p > < p > Username < br / > < asp:TextBox ID="TextBox1" Runat="server" >< /asp:TextBox > < /p > < p > Password < br / > < asp:TextBox ID="TextBox2" Runat="server" TextMode="Password" >< /asp:TextBox > < /p > < p > < asp:Button ID="Button1" Runat="server" Text="Login" OnClick="Button1_Click" / > < /p > < p > < asp:Label ID="Label1" Runat="server" >< /asp:Label > < /p > < /div > < /form > < /body > < /html > C# < %@ Page Language="C#" % > < script runat="server" > protected void Button1_Click(object sender, EventArgs e) { if (CheckBox1.Checked == true) Continued 782 Evjen c16.tex V2 - 01/28/2008 2:51pm Page 783 Chapter 16: Membership and Role Management { MembershipUser user = Membership.GetUser(TextBox1.Text); user.UnlockUser(); } if (Membership.ValidateUser(TextBox1.Text, TextBox2.Text)) { Label1.Text = "You are logged on!"; } else { MembershipUser user = Membership.GetUser(TextBox1.Text); Label1.Text = "Locked out value: " + user.IsLockedOut.ToString(); } } < /script > This page contains two text boxes: one for the username and another for the password. Above these, however, is a check box that can be used to unlock a user after you have locked down the account because of bad password attempts. If you run this page and enter three consecutive bad passwords for your user, you get the results pre- sented in Figure 16-10. Figure 16-10 The IsLockedOut property is read through an instantiation of the MembershipUser object. This object allows you programmatic access to the user data points contained in the aspnet_Membership table. In this case, the IsLockedOut property is retrieved and displayed to the screen. The MembershipUser 783 Evjen c16.tex V2 - 01/28/2008 2:51pm Page 784 Chapter 16: Membership and Role Management object also exposes a lot of available methods — one of which is the UnlockUser() method. This method is invoked if the check box is checked in the button-click event. Working with Authenticated Users After users are authenticated, ASP.NET 3.5 provides a number of different server controls and methods that you can use to work with the user details. Included in this collection of tools are the LoginStatus and the LoginName controls. The LoginStatus Server Control The LoginStatus server control enables users to click a link to log in or log out of a site. For a good example of this control, remove the < deny > element from the web.config filesothatthepagesofyour site are accessible to unauthenticated users. Then code your Default.aspx page so that it is similar to the code shown in Listing 16-18. Listing 16-18: Login and logout features of the LoginStatus control < %@ Page Language="VB" % > < html xmlns="http://www.w3.org/1999/xhtml" > < head runat="server" > < title > Login or Logout < /title > < /head > < body > < form id="form1" runat="server" > < asp:LoginStatus ID="LoginStatus1" Runat="server" / > < /form > < /body > < /html > Running this gives you a simple page that contains only a hyperlink titled Login ,asshownin Figure 16-11. Clicking the Login hyperlink forwards you to the Login.aspx page where you provide your credentials. After the credentials are provided, you are redirected to the Default.aspx page — although now the page includes a hyperlink titled Logout (see Figure 16-12). The LinkStatus control displays one link when the user is unauthenticated and another link when the user is authenticated. Clicking the Logout hyperlink logs out the user and redraws the Default.aspx page — but with the Login hyperlink in place. The LoginName Server Control The LoginName server control enables you to display the username of the authenticated user. This is a common practice today. For an example of this, change the Default.aspx page so that it now includes the authenticated user’s login name when that user is logged in, as illustrated in Listing 16-19. Listing 16-19: Displaying the username of the authenticated user < %@ Page Language="VB" % > < html xmlns="http://www.w3.org/1999/xhtml" > 784 Evjen c16.tex V2 - 01/28/2008 2:51pm Page 785 Chapter 16: Membership and Role Management < head runat="server" > < title > Login or Logout < /title > < /head > < body > < form id="form1" runat="server" > < asp:LoginStatus ID="LoginStatus1" Runat="server" / > < p >< asp:LoginName ID="LoginName1" Runat="server" Font-Bold="True" Font-Size="XX-Large" / >< /p > < /form > < /body > < /html > Figure 16-11 Figure 16-12 When the user logs in to the application and is returned to the Default.aspx page, he sees his username displayed, as well as the hyperlink generated by the LoginStatus control (see Figure 16-13). 785 Evjen c16.tex V2 - 01/28/2008 2:51pm Page 786 Chapter 16: Membership and Role Management Figure 16-13 In addition to just showing the username of the logged in user, you can also add text by using the LoginName control’s FormatString property. For instance, to provide a welcome message along with the username, you construct the LoginName control as follows: < asp:LoginName ID="LoginName1" Runat="Server" FormatString="Welcome to our Website {0}!" / > You can also simply use the following construction in one of the page events. (This is shown in VB; if you are using C#, add a semicolon at the end of the line.) LoginName1.FormatString = "Welcome to the site {0}!" When the page is generated, ASP.NET replaces the { 0 } part of the string with the username of the logged-in user. This provides you with a result similar to the following: Welcome to the site evjen! If you do not want to show the username when using the LoginName control, simply omit the { 0 } aspect of the string. The control then places the FormatString property’s value on the page. Showing the Number of Users Online One cool feature of the membership service is that you can display how many users are online at a given moment. This is an especially popular option for a portal or a forum that wishes to impress visitors to the site with its popularity. To show the number of users online, you use the GetNumberOfUsersOnline method provided by the Membership class. You can add to the Default.aspx page shown in Figure 16-10 with the code illustrated in Listing 16-20. Listing 16-20: Displaying the number of users online VB < %@ Page Language="VB" % > < script runat="server" > 786 . data points contained in the aspnet_Membership table. In this case, the IsLockedOut property is retrieved and displayed to the screen. The MembershipUser 7 83 Evjen c16.tex V2 - 01/28/2008 2 :51 pm. c16.tex V2 - 01/28/2008 2 :51 pm Page 781 Chapter 16: Membership and Role Management ASP. NET has built -in protection against this type of activity. If you look in the aspnet_Membership table, you. in the application, he is redirected to the Login.aspx page. You can see how ASP. NET tracks the location in the URL from the address bar in the browser: http://localhost:18 436 /Membership/Login.aspx?ReturnUrl=%2fMembership%2fDefault.aspx The

Ngày đăng: 05/07/2014, 19:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan