Once you've configured a default Membership provider for your Web application, it's trivial to program against it. There's a helper class called Membership that exposes much of the functionality of the MembershipProvider class, along with several overloaded methods that make certain methods easier to use (by passing default values for unused arguments). For example, if you want to get a list of users, you can simply call Membership.GetAllUsers(). Under the hood, this accesses the default provider and calls its GetAllUsers method, passing in the three arguments the provider expects to see, indicating that you don't care about paging; rather, you just want a collection of all the possible users. For some reason, the Membership class doesn't expose all of the functionality of MembershipProvider. For example, if you're implementing a form to allow the user to change her password question and answer, you'll quickly find that the Membership class doesn't expose the ChangePasswordQuestionAndAnswer method. But don't fret; you can just reach down and talk directly to the provider, and there's absolutely no harm in doing this. The easiest way to get your hands on the default provider for your application is to use the Membership.Provider property. This will load your provider based on the class name in web.config (if it's not already been loaded), and give you a reference to it. This is how the Membership class itself dispatches calls to the provider. The Membership class doesn't seem to be critical in this version of ASP.NET; it seems more of a convenience. You could live a full and rich life programming against MembershipProvider without ever using the Membership class. The only nonobvious thing is how to load the default provider, and as you can see in Listing 5-7, even that is not difficult, and can actually come in handy if you want to change some of the configuration settings before loading the provider.[13]
Listing 5-7. LoadMembershipProvider
|