ASP.NET membership is designed to enable you to easily use a number of different membership providers for your ASP.NET applications. You can use the supplied membership providers that are included with the .NET Framework, or you can implement your own providers.
There are two primary reasons for creating a custom membership provider.
-
You need to store membership information in a data source that is not supported by the membership providers included with the .NET Framework, such as a FoxPro database, an Oracle database, or other data sources.
-
You need to manage membership information using a database schema that is different from the database schema used by the providers that ship with the .NET Framework. A common example of this would be membership data that already exists in a SQL Server database for a company or Web site.
Required Classes
To implement a membership provider, you create a class that inherits the
Required ProviderBase Members
Member | Description |
---|---|
|
Takes, as input, the name of the provider and a |
Required MembershipProvider Members
Member | Description |
---|---|
|
A Boolean value specified in the configuration file (Web.config). The EnablePasswordReset property indicates whether users can use the This property is read-only. |
|
A Boolean value specified in the configuration file (Web.config). The EnablePasswordRetrieval property indicates whether users can retrieve their password using the This property is read-only. |
|
A Boolean value specified in the configuration file (Web.config). The RequiresQuestionAndAnswer property indicates whether users must supply a password answer in order to retrieve their password using the GetPassword method, or reset their password using the ResetPassword method. This property is read-only. |
|
A Boolean value specified in the configuration file (Web.config). The RequiresUniqueEmail property indicates whether users must supply a unique e-mail address value when creating a user. If a user already exists in the data source for the current This property is read-only. |
|
A The PasswordFormat property indicates the format that passwords are stored in. Passwords can be stored in Clear, Encrypted, and Hashed password formats. Clear passwords are stored in plain text, which improves the performance of password storage and retrieval but is less secure, as passwords are easily read if your data source is compromised. Encrypted passwords are encrypted when stored and can be decrypted for password comparison or password retrieval. This requires additional processing for password storage and retrieval but is more secure, as passwords are not easily determined if the data source is compromised. Hashed passwords are hashed using a one-way hash algorithm and a randomly generated salt value when stored in the database. When a password is validated, it is hashed with the salt value in the database for verification. Hashed passwords cannot be retrieved. You can use the This property is read-only. |
|
An Integer value specified in the configuration file (Web.config). The If the Invalid password and password answer attempts are tracked in the This property is read-only. |
|
An Integer value specified in the configuration file (Web.config). For a description, see the description of the MaxInvalidPasswordAttempts property. This property is read-only. |
ApplicationName property |
The name of the application using the membership information specified in the configuration file (Web.config). The ApplicationName is stored in the data source with related user information and used when querying for that information. See the section on the ApplicationName later in this topic for more information. This property is read/write and defaults to the |
CreateUser method |
Takes, as input, the name of a new user, a password, and an e-mail address and inserts a new user for the application into the data source. The CreateUser method returns a The |
|
Takes, as input, a MembershipUser object populated with user information and updates the data source with the supplied values. |
|
Takes, as input, the name of a user and deletes that user's information from the data source. The DeleteUser method returns true if the user was successfully deleted; otherwise, false. An additional Boolean parameter is included to indicate whether related information for the user, such as role or profile information is also deleted. |
ValidateUser method |
Takes, as input, a user name and a password and verifies that the values match those in the data source. The ValidateUser method returns true for a successful user name and password match; otherwise, false. |
|
Takes, as input, a unique user identifier and a Boolean value indicating whether to update the |
|
Takes, as input, a user name and a Boolean value indicating whether to update the LastActivityDate value for the user to show that the user is currently online. The GetUser method returns a MembershipUser object populated with current values from the data source for the specified user. If the user name is not found in the data source, the GetUser method returns null (Nothing in Visual Basic). |
|
Returns a The results returned by GetAllUsers are constrained by the pageIndex and pageSize parameters. The pageSize parameter identifies the maximum number of MembershipUser objects to return in the MembershipUserCollection. The pageIndex parameter identifies which page of results to return, where 1 identifies the first page. The totalRecords parameter is an |
|
Returns an integer value that is the count of all the users in the data source where the LastActivityDate is greater than the current date and time minus the |
ResetPassword method |
Takes, as input, a user name and a password answer and generates a new, random password for the specified user. The ResetPassword method updates the user information in the data source with the new password value and returns the new password as a string. A convenient mechanism for generating a random password is the The ResetPassword method ensures that the EnablePasswordReset property is set to true before performing any action. If the EnablePasswordReset property is false, a The ResetPassword method raises the ValidatePassword event, if a MembershipValidatePasswordEventHandler has been specified, to validate the newly generated password and continues or cancels the reset-password action based on the results of the event. You can use the |
GetPassword method |
Takes, as input, a user name and a password answer and retrieves the password for that user from the data source and returns the password as a string. GetPassword ensures that the EnablePasswordRetrieval property is set to true before performing any action. If the EnablePasswordRetrieval property is false, an The GetPassword method also checks the value of the RequiresQuestionAndAnswer property. If the RequiresQuestionAndAnswer property is true, the GetPassword method checks the value of the supplied answer parameter against the stored password answer in the data source. If they do not match, a MembershipPasswordException is thrown. |
|
Takes, as input, an e-mail address and returns the first user name from the data source where the e-mail address matches the supplied email parameter value. If no user name is found with a matching e-mail address, an empty string is returned. If multiple user names are found that match a particular e-mail address, only the first user name found is returned. |
ChangePassword method |
Takes, as input, a user name, a current password, and a new password, and updates the password in the data source if the supplied user name and current password are valid. The ChangePassword method returns true if the password was updated successfully; otherwise, false. The ChangePassword method raises the ValidatePassword event, if a MembershipValidatePasswordEventHandler has been specified, and continues or cancels the change-password action based on the results of the event. You can use the OnValidatePassword virtual method to execute the specified MembershipValidatePasswordEventHandler. |
ChangePasswordQuestionAndAnswer method |
Takes, as input, a user name, a password, a password question, and a password answer, and updates the password question and answer in the data source if the supplied user name and password are valid. The ChangePasswordQuestionAndAnswer method returns true if the password question and answer are updated successfully; otherwise, false. If the supplied user name and password are not valid, false is returned. |
|
Returns a list of membership users where the user name contains a match of the supplied usernameToMatch for the configured ApplicationName. For example, if the usernameToMatch parameter is set to "user," then the users "user1," "user2," "user3," and so on are returned. Wildcard support is included based on the data source. Users are returned in alphabetical order by user name. The results returned by FindUsersByName are constrained by the pageIndex and pageSize parameters. The pageSize parameter identifies the number of MembershipUser objects to return in the MembershipUserCollection. The pageIndex parameter identifies which page of results to return, where 1 identifies the first page. The totalRecords parameter is an |
|
Returns a list of membership users where the user name contains a match of the supplied emailToMatch for the configured ApplicationName. For example, if the emailToMatch parameter is set to "address@example.com," then users with the e-mail addresses "address1@example.com," "address2@example.com," and so on are returned. Wildcard support is included based on the data source. Users are returned in alphabetical order by user name. The results returned by FindUsersByEmail are constrained by the pageIndex and pageSize parameters. The pageSize parameter identifies the number of MembershipUser objects to return in the MembershipUserCollection collection. The pageIndex parameter identifies which page of results to return, where 1 identifies the first page. The totalRecords parameter is an |
|
Takes, as input, a user name, and updates the field in the data source that stores the IsLockedOut property to false. The UnlockUser method returns true if the record for the membership user is updated successfully; otherwise false. |
ApplicationName
Membership providers store user information uniquely for each application. This enables multiple ASP.NET applications to use the same data source without running into a conflict if duplicate user names are created. Alternatively, multiple ASP.NET applications can use the same user data source by specifying the same ApplicationName.
Because membership providers store user information uniquely for each application, you will need to ensure that your data schema includes the application name and that queries and updates also include the application name. For example, the following command is used to retrieve a user name from a database, based on the e-mail address, and ensures that the ApplicationName is included in the query.
В | Copy Code |
---|---|
SELECT Username FROM MyUserTable WHERE Email = 'someone@example.com' AND ApplicationName = 'MyApplication' |
Custom Members
You may need to extend the membership provider interfaces with additional functionality not provided by the ProviderBase and MembershipProvider abstract classes. Any public members that you add to your membership provider will be accessible using the
An example of this could be a LockUser method that sets the IsLockedOut property to true. The following example shows how to cast the Provider property, which exposes the default membership provider for an application, as a custom-provider type in order to call the custom LockUser method.
Visual BasicВ | Copy Code |
---|---|
Dim p As MyCustomProvider = CType(Membership.Provider, MyCustomProvider) p.LockUser(username) |
C#В | Copy Code |
---|---|
MyCustomProvider p = (MyCustomProvider)Membership.Provider; p.LockUser(username); |
Thread Safety
For each membership provider specified in the configuration for an application, ASP.NET instantiates a single membership provider instance that is used for all of the requests served by an