I have seen and also got several questions about how to extend the MemberhsipUser class with some extra properties. I hope this post can give you some ideas.
As you may all know, the MembershipUser is a class with very few properties, for example it has no FirstName or LastName etc. This kind of information can be something you like to add or use together with the Membership feature. You can use the Profile feature to simply extend information about a user, but the problem is that you will have two classes, the MembershipUser and the Profile class that will together form a user. Microsoft will probably add a solution to the next version of ASP.Net to make it possible to easy extend the MembershipUser class, probably a solution similar to the Profile feature. But this is nothing they haven’t promised. But back to the MembershipUser class. You can inherit the MembershipUser class and extend it with your own properties:
Public class MyCustomMebershipUser : MembershipUser
{
public string FirstName
{
get { ...}
set { ...}
}
....
}
To make it possible for you to use your own custom membership class, you need to create your own Membership provider. You do that by inheriting the MembershipProvider class an implement all its methods. You can also inherit the SqlMembershipProvider if you still want to use the data base created by Microsoft, but get some extra data for your custom membership user from a different data source. If you do so, make sure you, at least override the method that returns a MembershipUser class, and return your custom membership instead. The following code is an example of the GetUser method overridden from the SqlMemberhshipProvider class:
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
MemberhsipUser oldUser = base.GetUser(providerUserKey, userIsOnline);
//get extra data here..
string firstName = ...
MyCustomMemberhsipUser newUser = new MyCustomMembershipUser(oldUser.Username, .... , firstName);
return newUser;
}
The MemberhsipUser is part of an immutable class (can’t change its state without creating a new instance of the class). So make sure you follow the same pattern. Extend your custom MembershipUser class with a new constructor that will take all the arguments needed to fill your class with data. Some of the MembershpUser properties are both get and set methods, but the UserName is only a read-only property.
When you use the Membership class to get a MembershipUser where your custom provider is used, you need to cast the returned MembershipUser class into your class. The following is an example of where the GetUser method of the Membership class is used and will return a MembershipUser class that you need to cast to your type, only to make it possible to access your extended properties:
MyCustomMembershipUser user = (MyCustomMembershipUser)Memberhisp.GetUser();
FirstNameLabel.Text = user.FirstName;
I hope this post will give you some basic ideas how you can extend the MembershipUser with your own properties.