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


Microsoft ASP.NET 2.0 Membership API Extended : Page 2

Working with big applications requires extending the Microsoft ASP.NET 2.0 Membership API to handle more detailed member records.

The Concept
The idea behind extending the Membership API is as follows: I'll create a new ExtendedMembershipUser class that inherits all the default properties from the MembershipUser, and then I'll add my own custom properties that I will use throughout the article to explain one of the methodologies you can use to extend the ASP.NET 2.0 Membership API. You can later customize this object to fit your needs and requirements.

The new object contains three added properties: FirstName, LastName, and DateOfBirth. This snippet shows a sample of the ExtendedMembershipUser class you'll learn about in this article:

   public ExtendedMembershipUser(MembershipUser _mu,
      string FirstName, 
      string LastName, 
      DateTime DateOfBirth)
      : base(_mu.ProviderName, _mu.UserName, 
         _mu.Email, _mu.PasswordQuestion, _mu.Comment, 
         _mu.IsApproved, _mu.IsLockedOut, _mu.CreationDate,
         _mu.LastLoginDate, _mu.LastActivityDate, 
      // Assign local fields
      this._FirstName = FirstName;
      this._LastName = LastName;
      this._DateOfBirth = DateOfBirth;
As you can see, the ExtendedMembershipUser object inherits from the MembershipUser class. The constructor's input is an object of type MembershipUser and the three added properties mentioned above. In this case, the constructor makes a call to the base class inheriting from the MembershipUser.

The full constructor shown in the code snippet below has as input a list of all the individual properties of the MembershipUser together with the custom properties that I added.

   public ExtendedMembershipUser(
      string ProviderName, string UserName, 
      object ProviderUserKey, string Email, 
      string PasswordQuestion, string Comment, 
      bool IsApproved, bool IsLockedOut, 
      DateTime CreationDate, DateTime LastLoginDate, 
      DateTime LastActivityDate, DateTime LastPasswordChangedDate,
      DateTime LastLockoutDate, string FirstName,
      string LastName, DateTime DateOfBirth)
      : base(ProviderName, 
         UserName, ProviderUserKey, Email, 
         PasswordQuestion, Comment, IsApproved, 
         IsLockedOut, CreationDate, LastLoginDate, 
         LastActivityDate, LastPasswordChangedDate, LastLockoutDate)
      // Assign local fields
      this._FirstName = FirstName;
      this._LastName = LastName;
      this._DateOfBirth = DateOfBirth;
The next step is to decide what should go in the ExtendedMembership class, which is the provider model's manager class that should include the static methods available to manage the custom ExtendedMembershipUser. Basically, you need to upgrade the CreateUser, Update, Delete, GetUser, and GetAllUsers methods, which you will mostly use in a web application. Why are these methods the only methods you need to upgrade?

The answer is simple if you look at all the other methods available in the Membership class:

  • FindUsersByEmail: I doubt this method is widely used. How many times do you allow users to have the same e-mail address in your application? So you don't need to upgrade this method.
  • FindUsersByName: Do you often allow members in the same application to have the same UserName? Probably not. So once again, you don't need to upgrade this one.
  • FindUserNameByEmail: You do not need to upgrade this method. You can use it "as is" since you can retrieve the UserName from the Membership API database regardless of what custom properties you add to the database.
So now you know the required methods and the main ExtendedMembershipUser to work with, it is time to start developing this new API.

One important thing to mention before proceeding is that you will build the extended Membership API upon a provider model just as Microsoft built the Membership API.

You'll use the provider model mainly to build the additional features required to process the new custom properties you added to the ExtendedMembershipUser class. Therefore, the implementation of the methods inside the ExtendedMembership class contains calls to both the default Membership methods (represented by built-in methods such as Membership.GetUser, Membership.CreateUser, etc.) and you'll create the methods implemented by the new provider model to handle the custom properties added.

After developing the new API, you will have a rich environment to work with because you can use both APIs: one to handle the default MembershipUser combined with the custom added properties and one to handle routine functions, such as GetPassword, FindUserNameByEmail, and ValidateUser.

To sum up this section, you will use the new extended Membership via the Create, Update, Delete, Get, and GetAll methods now that you have integrated the built-in MembershipUser and the custom properties. For example, adding a new member requires that you add data to the tables that ship with the Membership API and to the table that holds the additional member's data. You can still use all the other built-in functionalities from the Membership class. For example, the Login control automatically validates a user the same way since you did not change the ValidateUser method.

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