Programming Microsoft ASP.NET 4 - Dino Esposito [424]
public class MyAppMembershipProvider : MembershipProvider
{
// Implements all abstract members of the class and, if
// needed, defines custom functionality
...
}
This approach can be successfully employed to migrate existing authentication code to newer versions of ASP.NET applications and, perhaps more importantly, to link a custom and existing data store to the membership API. We’ll return to this subject in a moment.
The ProviderBase Class
All the providers used in ASP.NET—not just membership providers—implement a common set of members: the members defined by the ProviderBase class. The class comes with one method, Initialize, and one property, Name. The Name property returns the official name of the provider class. The Initialize method takes the name of the provider and a name/value collection object packed with the content of the provider’s configuration section. The method is supposed to initialize its internal state with the values just read out of the web.config file.
The MembershipProvider Class
Many of the methods and properties used with the Membership class are actually implemented by calling into a corresponding method or property in the underlying provider. It comes as no surprise, then, that many of the methods listed in Table 19-10, which are defined by the MembershipProvider base class, support the functions you saw in Table 19-9 that are implemented by the dependent Membership class. However, note that Table 19-9 and Table 19-10 are very similar but not identical.
Table 19-10. Methods of the MembershipProvider Class
Method
Description
ChangePassword
Takes a user name in addition to the old and new password, and changes the user’s password.
ChangePasswordQuestionAndAnswer
Takes a user name and password, and changes the pair of question/answer challenges that allows reading and changing the password.
CreateUser
Creates a new user account, and returns a MembershipUser-derived class. The method takes the user name, password, and e-mail address.
DeleteUser
Deletes the record that corresponds to the specified user name.
FindUsersByEmail
Returns a collection of membership users whose e-mail address corresponds to the specified e-mail.
FindUsersByName
Returns a collection of membership users whose user name matches the specified user name.
GetAllUsers
Returns the collection of all users managed by the provider.
GetNumberOfUsersOnline
Returns the number of users that are currently considered to be on line.
GetPassword
Takes the user name and the password’s answer, and returns the current password for the user.
GetUser
Returns the information available about the specified user name.
GetUserNameByEmail
Takes an e-mail address, and returns the corresponding user name.
ResetPassword
Takes the user name and the password’s answer, and resets the user password to an auto-generated password.
UpdateUser
Updates the information available about the specified user.
ValidateUser
Validates the specified credentials against the stored list of users.
All these methods are marked as abstract virtual in the class (must-inherit, overridable in Visual Basic .NET jargon). The MembershipProvider class also features a few properties. They are listed in Table 19-11.
Table 19-11. Properties of the MembershipProvider Class
Property
Description
ApplicationName
Returns the provider’s nickname.
EnablePasswordReset
Indicates whether the provider supports password reset.
EnablePasswordRetrieval
Indicates whether the provider supports password retrieval.
MaxInvalidPasswordAttempts
Returns the maximum number of invalid password attempts allowed before the user is locked out.
MinRequiredNonAlphanumericCharacters
Returns the minimum number of punctuation characters required in the password.
MinRequiredPasswordLength
Returns the minimum required length for a password.
PasswordAttemptWindow
Returns