|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
How to: Implement a Custom Membership User
Provides an example that illustrates how to extend theclass with a custom membership provider.
While the userprovides a convenient mechanism for storing information per user in a Web application, the design of your application might require that additional user information be stored with the user authentication information in the data store. In this case, you would need to build a custom membership provider to store and retrieve the user authentication information and the additional user values in your data store (for an example of a custom membership provider, see ). Additionally, you can extend the MembershipUser class to make the added user values available to application code.
Creating a custom membership user involves the following tasks:
Create a class that inherits the MembershipUser class.
Create a data source to store authentication information and additional user settings.
Create a custom membership provider for the data store. The custom membership provider will contain additional code that can receive objects of the custom membership user type as input, as well as return objects of the custom membership user type.
The examples in this topic show how you can modify the custom membership provider example into support a custom membership user implementation.
Create a Custom Membership User
You can create a custom membership user by creating a class that inherits the MembershipUser class, and then including properties that expose the additional user values. Optionally, you can add methods and events to the MembershipUser class as well.
When the Membership class is called to create an instance of your custom MembershipUser, only the constructors defined by the MembershipUser class will be called. If your MembershipUser implementation includes additional constructor overloads, those constructors will only be called by application code that is written specifically to call a custom constructor.
The following code example shows a simple custom membership user that inherits the MembershipUser class and provides two additional properties:
IsSubscriber, which is a Boolean property that identifies whether the user subscribes to a service or newsletter for a Web application; and
CustomerID, which contains a unique identifier for a separate customer database.
For an example of modifying thecontrol to include additional user information for a membership user, see .
Create a Data Store for the Membership User Data
You will need to provide a data store for the user authentication information for the membership feature, as well as the additional user information for your custom membership user.
The following code example shows a query that you can run in a Microsoft Access database to create a table to store authentication information and property values for your custom membership user.
CREATE TABLE Users ( PKID Guid NOT NULL PRIMARY KEY, Username Text (255) NOT NULL, ApplicationName Text (255) NOT NULL, Email Text (128) NOT NULL, Comment Text (255), Password Text (128) NOT NULL, PasswordQuestion Text (255), PasswordAnswer Text (255), IsApproved YesNo, LastActivityDate DateTime, LastLoginDate DateTime, LastPasswordChangedDate DateTime, CreationDate DateTime, IsOnLine YesNo, IsLockedOut YesNo, LastLockedOutDate DateTime, FailedPasswordAttemptCount Integer, FailedPasswordAttemptWindowStart DateTime, FailedPasswordAnswerAttemptCount Integer, FailedPasswordAnswerAttemptWindowStart DateTime, IsSubscriber YesNo, CustomerID Text (64) )
Create a Custom Membership Provider
You will need to create a custom membership provider that supports both your custom membership user type, and your custom membership data store. Theand methods of the custom membership provider can be written to return objects of the custom membership user type. The method of the custom membership provider can be written to receive an object of the custom membership user type as input.
The following sections provide guidance on creating a custom membership provider that uses a custom membership user type. The examples build on the code provided inand use the database schema from the section earlier in this topic.
Modify the GetUser Methods
When working with a custom membership user type, theand methods of your membership provider must still return an object of type MembershipUser. Provided your custom membership user class inherits the MembershipUser class, return an object of your custom membership user type as the return value for your implementation of the GetUser methods. Application code can then cast the returned MembershipUser as your custom membership user type to access the additional members of your custom membership user as shown in the following code example.
The following code example shows the modified GetUser methods (and their supporting private method) of the sample membership provider from, which have been updated to return the custom membership user type from the section earlier in this topic.
Modify the UpdateUser Method
When working with a custom membership user type and a custom membership provider, implement an UpdateUser method that takes an object of type MembershipUser as input. In your implementation of the UpdateUser method, cast the supplied MembershipUser object as your custom membership user type to access the values of the additional properties and update them in the data store.
The following code example shows the modified UpdateUser method of the sample membership provider from, which has been updated to cast the supplied user as the custom membership user type from the section earlier in this topic.
Modify the CreateUser Method
When working with a custom membership user type and a custom membership provider, the custom membership provider must implement a CreateUser method that takes only the properties supported by the MembershipUser class as input. You can create an overload of the CreateUser method that takes additional property values as shown in the following code example.
However, this overload will not be called by the Membership class or controls that rely on the Membership class, such as the CreateUserWizard control. To call this method from an application, cast theinstance referenced by the Membership class as your custom membership provider type, and then call your CreateUseroverload directly.
If your application is using the CreateUserWizard control to add new users to your membership data source, you can customize the wizard steps of the CreateUserWizard control to include controls that retrieve the additional property values of your custom membership user. You can then handle theevent of the CreateUserWizard control and add event code that does the following:
Retrieves the property values of the additional membership user.
Casts the membership user created by the CreateUserWizard control as your custom membership user type.
Sets the additional properties on the membership user.
Passes the updated user to themethod of the Membership class. This will call the UpdateUser method of your custom provider (which is described in the section earlier in this topic) to add the additional property values to your data source.
For an example of modifying the steps of the CreateUserWizard, see.
The following code example shows the modified CreateUser method of the sample membership provider from, which has been updated to return the custom membership user type from the section earlier in this topic. An overload has been created to take values for the additional properties of the custom membership provider as input.