RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Writing A Custom Membership Provider for your ASP.NET 2.0 Web Site : Page 2

If you have an ASP.NET Web site that stores any registration or log in information for your users, you probably know that the Membership provider in ASP.NET 2.0 uses a SQL Server Express 2005 database. But what if you want to use something else? This article will show you to use your own data source for storing membership information.

Creating the Project
As there are numerous controls in the Login controls family that use the membership provider, for simplicity I will only implement a subset of the features available in the membership provider. For this project, I will implement the:
    CreateUser() function—this function is used by the CreateUserWizard control to allow users to register for a new account.
  • ValidateUser() function—this function is used by the Login control to allow users to authenticate themselves.
Using Visual Studio 2005 Beta 2, create a new Web site project using the Visual Basic language. Save the project as C:\NewMembershipProvider. Populate the Default.aspx Web form with the CreateUserWizard control (see Figure 2) and apply the Elegant scheme using the Auto Format link in the Smart tag of the control.

Figure 2. Setting it Up: The CreateUserWizard control is shown using the Elegant scheme, which quickly sets up a page to register a new user.
Figure 3. Log Services: The LoginView and LoginStatus controls are added to the AnonymousTemplate view in order to give the user a way to log in and out of the service.

Set the ContinueDestinationPageUrl property to "Login.aspx" so that when the user account is created, the user is redirected to the Login.aspx page(we will add this page shortly).

Also, add the LoginView control as shown in Figure 3 to the bottom of Default.aspx. In the AnonymousTemplate view of the LoginView control, add the LoginStatus control. The LoginView control allows you to display different messages depending on whether the user is authenticated (messages to display are set in the LoggedInTemplate view; see below) or not (messages are set in the AnonymousTemplate view). The LoginStatus control displays a hyperlink to redirect the user to a login page if he is not authenticated and allows the user to logout if he is authenticated.

In the LoggedInTemplate view of the LoginView control, type the message "You are logged in as" and add the LoginName and LoginStatus control as shown in Figure 4. The LoginName control will display the user's login ID.

Figure 4. Username View: Adding the LoginName and LoginStatus controls in LoggedInTemplate view will display the username of a logged in user.
Figure 5. Logging In: The Login.aspx page uses only the Login control, also with the Elegant scheme applied, to allow users who already have accounts to authenticate.

Next, add a new Web form to the project and name it Login.aspx. Add the Login control to the page (see Figure 5). Apply the Elegant scheme to the control.

Creating the Database
The custom provider you are building will save its data into an Access database. Therefore, you need to create an Access database. To maintain consistency with the location of databases in a typical ASP.NET 2.0 web application, save this database in C:\NewMembershipProvider\App_Data\. Give it the name Members.mdb.

Create a new table in the Members.mdb database and name it Membership. Define the table as shown in Table 1. :

Table 1. Fields for the Membership table of memers.mdb.

Field Name Data Type Field Size
Username (key) Text 8
Password Text 8
Email Text 50
passwordQuestion Text 50
passwordAnswer Text 50

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date