Using the Membership Class to Access Your Provider






Using the Membership Class to Access Your Provider

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]

[13] Most provider settings are read-only once the provider is loaded, but you can use the ProviderSettings.Parameters collection to tweak the settings from config. Just be sure to make all your changes before calling ProvidersHelper.InstantiateProvider(). Also be aware that any tweaks you make may limit an administrator's ability to configure your application by changing your web.config file.

-7. LoadMembershipProvider

using System.Configuration;
using System.Web.Configuration;
using System.Web.Security;
// ...
public MembershipProvider LoadMembershipProvider(string name) {
  MembershipSection ms = (MembershipSection)
    WebConfigurationManager.GetSection("system.web/membership");
  foreach (ProviderSettings ps in ms.Providers) {
    if (name == ps.Name) {
      return (MembershipProvider)ProvidersHelper.InstantiateProvider(
        ps, typeof(MembershipProvider));
    }
  }
  return null; // not found
}



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows