After Microsoft released ASP.NET 1.0 the team members immediately started looking for areas where they could simplify. One of the areas that came up was the management of user credentials, personalization, and user roles. All of these were problems that could be solved in ASP.NET 1.1, but they wanted to make it better . . . and easier! The Membership feature of ASP.NET does just thatmakes it better and easier. Membership provides secure credential storage with simple, easy-to-use APIs. Rather than requiring you to repeatedly develop infrastructure features for authenticating users, it is now part of the platform. Forms Authentication and Membership complement one another. However, they can also act independently, that is, you don't have to use them together. The code sample in Listing 6.2 demonstrates how Membership is used with Forms Authentication. Listing 6.2 Using the Membership API<script runat="server"> Public Sub Login_Click(sender As Object, e As EventArgs e) ' Is the user valid? ' If (Membership.ValidateUser (Username.Text, Password.Text)) Then FormsAuthentication.RedirectFromLoginPage (Username.Text, false) Else Status.Text = "Invalid Credentials: Please try again" End If End Sub </script> <html> <body style="FONT-FAMILY: Verdana"> <H1>Enter your username/password</H1> <form id="Form1" runat="server"> Username: <asp:textbox id="Username" runat="server" /> <br> Password: <asp:textbox id="Password" runat="server" /> <p> <asp:button id="Button1" text="Check if Member is Valid" onclick="Login_Click" runat="server"/> </form> <font color="red" size="6"> <asp:label id="Status" runat="server"/> </font> </body> </html> As you can see, our custom code to validate the credentials is now replaced with a single call to the static Membership.ValidateUser() method. The code is also much cleaner and more readable as a resultand much more concise ! The Membership class contains only static methods . You don't have to create an instance of the class to use its functionality; for example, you don't have to new the Membership class to use it. Behind the scenes the Membership class is forwarding the calls through a configured provider. The provider in turn knows which data source to contact and how to verify the credentials (see Figure 6.2). Figure 6.2. The provider model
Providers are a new "design pattern" introduced with ASP.NET 2.0. Providers are pluggable data abstraction layers used within ASP.NET. All ASP.NET 2.0 features that rely on data storage expose a provider layer. The provider layer allows you to take complete control over how and where data is stored. [1]
Membership ProvidersThe beauty of the provider model is the abstraction that it affords the developer. Rather than being pigeonholed into a particular data model, the provider pattern allows the developer to determine how and where the actual data storage takes place. ASP.NET 2.0 will ship with several providers for Membership (not a complete list):
You can also author your own provider and plug it in. The provider design pattern allows for one common API that developers can familiarize themselves with, such as Membership , but under the covers you still have control over what exactly is happening. For example, if you have all of your customer information stored in an AS/400, you could write a provider for Membership. Users would call the familiar Membership APIs, but the work would actually be handled by the configured AS/400 provider. The goal of Membership is to simplify managing and storing user credentials while still allowing you to control your data, but it does much more. Let's dig deeper. Setting Up MembershipSetting up Membership is easy: It just works. By default all the providers that ship with ASP.NET 2.0 use a Microsoft Access provider and will use the default AspNetDB.mdb file created in the \data\ directory of your application. [3]
If the \data\ directory of your application does not exist, ASP.NET will attempt to create it. If ASP.NET is unable to create the \data\ directory or the AspNetDB.mdb file due to security policy on the machine, an exception is thrown detailing what needs to be done. Before we can begin using Membership for its most common task validating user credentialswe need to have users to validate! Creating a New UserThe Membership API exposes two methods for creating new users: CreateUser( username As String , password As String) CreateUser( username As String , password As String , email As String) These two APIs are somewhat self-explanatory. We call them to create a user with a username and passwordoptionally also providing the e-mail address. Both of these methods return a MembershipUser instance, which we'll look at later in this chapter. Which of these two methods you use is determined by the Membership configuration settings. We can examine the settings in machine.config for the defaults (see Listing 6.3). [4]
Table 6.1. Configuration Elements for the Membership Provider
Listing 6.3 Membership Configuration <configuration> <system.web> <membership defaultProvider="AspNetAccessProvider" userIsOnlineTimeWindow="15"> <providers> <add name="AspNetAccessProvider" type="System.Web.Security.AccessMembershipProvider, System.Web, Version=1.1.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="AccessServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" appName="/" requiresUniqueEmail="false" passwordFormat="Hashed" /> </providers> </membership> </system.web> </configuration> Table 6.1 shows an explanation of the various configuration settings. Knowing what the defaults are, we can write a simple page for creating new users (see Listing 6.4). Listing 6.4 Creating Users with the Membership API<%@ Page Language="VB" %> <script runat="server"> Public Sub CreateUser_Click (sender As Object, e As EventArgs) Try ' Attempt to create the user Membership.CreateUser(Username.Text, Password.Text) Status.Text = "Created new user: " & Username.Text Catch ex As MembershipCreateUserException ' Display the status if an exception occurred Status.Text = ex.ToString() End Try End Sub </script> <html> <head> </head> <body style="FONT-FAMILY: Verdana"> <H1>Create a new user</H1> <hr /> <form runat="server"> Desired username: <asp:TextBox id="Username" runat="server"/> <br> Password: <asp:TextBox id="Password" runat="server" /> <p> <asp:button Text="Create Member" OnClick="CreateUser_Click" runat="server"/> </form> <font color="red" size="6"> <asp:Label id="Status" runat="server" /> </font> </body> </html> The code in Listing 6.4 calls the Membership.CreateUser() method, which accepts a username and a password. [5] If there is a problem creating the user, a MembershipCreateUserException is thrown. If there are no problems, the new user is created.
Once we've created some users, we can test the Membership.ValidateUser() method. Validating User CredentialsAs stated earlier, the primary purpose for Membership is to validate credentials. This is accomplished through the static ValidateUser() method: ValidateUser( username As String , password As String) As Boolean We can use this method, as seen earlier, along with Forms Authentication to validate user credentials. Here is a partial code example: If (Membership.ValidateUser (Username.Text, Password.Text)) Then FormsAuthentication.RedirectFromLoginPage (Username.Text, False) Else Status.Text = "Invalid Credentials: Please try again" End If Apart from ValidateUser() , most of the remaining Membership APIs are used for retrieving a user or users. Retrieving a UserThere are a few ways you can retrieve users that have already been created: GetUser() As MembershipUser GetUser( userIsOnline As Boolean) As MembershipUser GetUser( username As String) As MembershipUser GetUser( username As String , userIsOnline As Boolean) As MembershipUser The first two methods that don't have a username parameter will attempt to return the currently logged on user. The parameter userIsOnline , when set to True , will update a timestamp in the data store indicating the date/time the user was last requested . This timestamp can then be used to calculate the total number of users on-line. [6] The remaining methods will perform similar operations but on a specified user.
Figure 6.3 shows an example of getting the MembershipUser class for the currently logged on user. Figure 6.3. Getting a user
Listing 6.5 provides the code used for this page. Listing 6.5 Fetching the Logged on User<%@ Page Language="VB" %> <script runat="server"> Public Sub Page_Load() Dim user As MembershipUser ' Get the currently logged on user and ' update the user's on-line timestamp user = Membership.GetUser(True) UserName.Text = user.Username End Sub </script> <html> <body style="FONT-FAMILY: Verdana"> <H1>Get User</H1> <hr /> <form runat="server"> The currently logged on user is: <asp:literal id="UserName" runat="server" /> </form> </body> </html> If we want to find a user but don't have the username (e.g., the user forgot his or her username), we can use the GetUserNameByEmail() method: GetUserNameByEmail(email As String) As String Once we have the username, we can then look up the user with one of the GetUser() methods listed earlier. We can additionally get multiple users with the following method: Membership.GetAllUsers() As MembershipUserCollection
Membership.GetAllUsers() simply returns a MembershipUser Collection , which we can use to enumerate users or bind to a server control, such as a Repeater or DataGrid (see Figure 6.4). Figure 6.4. Getting all users
Listing 6.6 shows the code. Listing 6.6 Displaying All Users<%@ Page Language="VB" %> <script runat="server"> Public Sub Page_Load() Users.DataSource = Membership.GetAllUsers() Users.DataBind() End Sub </script> <html> <head> </head> <body style="FONT-FAMILY: Verdana"> <H1>Users in Membership Database</H1> <hr /> <asp:repeater id="Users" runat="server"> <headertemplate> <table border="1"> <tr> <td bgcolor="black" style="color:white"> Username </td> <td bgcolor="black" style="color:white"> Email </td> <td bgcolor="black" style="color:white"> Is Online </td> <td bgcolor="black" style="color:white"> Is Approved </td> <td bgcolor="black" style="color:white"> Date Last Active </td> <td bgcolor="black" style="color:white"> Date Created </td> <td bgcolor="black" style="color:white"> Date Password Changed </td> <td bgcolor="black" style="color:white"> Password Question </td> </tr> </headertemplate> <itemtemplate> <tr> <td> <%# Eval("Username") %> </td> <td> <%# Eval("Email") %> </td> <td> <%# Eval("IsOnline") %> </td> <td> <%# Eval("IsApproved") %> </td> <td> <%# Eval("LastLoginDate") %> </td> <td> <%# Eval("LastActivityDate") %> </td> <td> <%# Eval("CreationDate") %> </td> <td> <%# Eval("LastPasswordChangedDate") %> </td> <td> <%# Eval("PasswordQuestion") %> </td> </tr> </itemtemplate> <footertemplate> </table> </footertemplate> </asp:repeater> </body> </html> Now that we've looked at how to create users and retrieve named users, let's look at the MembershipUser class, which allows us to set and retrieve extended properties for each user. The MembershipUser ClassThe MembershipUser class represents a user stored in the Membership system. It provides the following methods for performing user-specific operations, such as retrieving or resetting a user's password. GetPassword() As String GetPassword( answer As String) As String ChangePassword( oldPassword As String , newPassword As String) As Boolean ChangePasswordQuestionAndAnswer( password As String , question As String , answer As String) As Boolean ResetPassword() As String ResetPassword( answer As String) As String Note that if a question and answer are being used, the overloaded GetPassword( answer As String) requires the case-insensitive question answer. The ChangePassword() method allows changes to the user's password, and the ChangePasswordQuestionAndAnswer() method allows changes to the user's password question and answer. The code in Listing 6.7 allows the currently logged on user to change his or her password question and answer. [7]
Listing 6.7 Changing a Password<%@ Page Language="VB" %> <script runat="server"> Public Sub Page_Load() If Not Page.IsPostBack Then DisplayCurrentQuestion() End If End Sub Public Sub SetQandA_Click(sender As Object, e As EventArgs) Dim u As MembershipUser = Membership.GetUser() u.ChangePasswordQuestionAndAnswer(CurrentPassword.Text, _ Question.Text, _ Answer.Text) Membership.UpdateUser(u) DisplayCurrentQuestion() End Sub Public Sub DisplayCurrentQuestion() Status.Text = Membership.GetUser().PasswordQuestion End Sub </script> <html> <body style="FONT-FAMILY: Verdana"> <H1>Set Question Answer</H1> <hr /> <form id="Form1" runat="server"> Current Password: <asp:textbox id="CurrentPassword" runat="server" /> <p></p> Question: <asp:textbox id="Question" runat="server" /> <p></p> Answer: <asp:textbox id="Answer" runat="server" /> <p></p> <asp:button id="Button1" text="Set Question/Answer" onclick="SetQandA_Click" runat="server"/> </form> <font size="6"> Your new password question is: <asp:label id="Status" runat="server"/> </font> </html> The ResetPassword() methods are similar to the GetPassword() methods. However, rather than retrieving the user's password, they reset and then return a random password for the user. Keep in mind that the ability to retrieve, change, or reset the user's password is determined by the settings within the configuration. In addition to password management, the MembershipUser class has some useful properties that provide us some details about how and when the user last logged in, last changed passwords, and so on (see Table 6.2). Updating a User's PropertiesWhen changes are made to the user, for example, updating the user's e-mail address, we need to use the Membership.UpdateUser( user As Membership User) method to save the values. [8] For example, in Listing 6.7 above, the SetQandA_Click event (repeated for convenience below) shows an example of Membership.UpdateUser() :
Public Sub SetQandA_Click(sender As Object, e As EventArgs) Dim u As MembershipUser = Membership.GetUser() u.ChangePasswordQuestionAndAnswer(CurrentPassword.Text, Question.Text, Answer.Text) Membership.UpdateUser(u) DisplayCurrentQuestion() End Sub Table 6.2. MembershipUser Properties
So far we've learned how to create and update users, but what about removing users from the Membership system? Deleting a UserDeleting a user from Membership is easy. Membership supports a single method for removing users: DeleteUser( username As String) As Boolean We simply need to name the user we wish to delete. If the operation is successful, the method returns True . If the delete operation fails, for example, if the user doesn't exist, False is returned. Listing 6.8 shows a code example that allows us to specify a user to be removed from the Membership system. Listing 6.8 Deleting a User<%@ Page Language="VB" %> <script runat="server"> Public Sub DeleteUser_Click(sender As Object, e As EventArgs) If (Membership.DeleteUser(Username.Text)) Then Status.Text = Username.Text & " deleted" Else Status.Text = Username.Text & " not deleted" End If End Sub </script> <html> <head> </head> <body style="FONT-FAMILY: Verdana"> <H1>Delete a user</H1> <hr /> <form runat="server"> Username to delete: <asp:TextBox id="Username" runat="server"/> <p> <asp:button Text="Delete User" OnClick="DeleteUser_Click" runat="server"/> </form> <font color="red" size="6"> <asp:label id="Status" runat="server" /> </font> </body> </html> Figure 6.5 shows how this page looks. Figure 6.5. Deleting a user
While the Membership APIs definitely simplify day-to-day tasks , there is also an alternative to using programmatic APIs: security server controls. In many cases we can use these server controls and never have to write code that uses the Membership APIs! |