views:

293

answers:

1

Hi,

I am trying to implement the provider pattern in a custom web control. I am still trying to understand the pattern and I have the following questions.

Is the default provider the provider that is always going to be used when my control loads? From what I can tell the provider used will always be the default but I am not sure because the MSDN documentation says that server controls that allow selection of Providers should have Provider property that should default to the value of the defaultProvider. To me that means the control can load a a given provider based on it's Provider property as long as that provider is in the Config file. Is that correct? So the default provider is returned if no specific provider is requested.

To change providers do I just simply change the defaultprovider in my config file? <-- I think that is incorrect as that would just change the defaultProvider returned.

Is this correct: In a control then the defaultprovider would be loaded in the OnLoad event method of the control? I need to pass the control data from different sources depending on the provider but the resulting data would then be put into a class. that the control would use to render itself.

I am also confused how to get the data to the control from the page? Say my ProviderBase had a method called LoadData

Then in my SQLCustomerProvider I would implement the LoadData method, is it ok to customize this code here to read my specific data and load into the class or should the page using the control load the data into the class?

Thanks!

A: 

I'll admit up front that my knowledge of providers is limited to writing them for Membership/Profile/Roles, but the general stuff should still apply:

Unless you allow the developer the oppertunity to specify a different provider, then yes, the default provider is the one that is going to be used.

So, taking authentication as an example, if you had the following in your web.config:

<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="20">
  <providers>
    <remove name="AspNetSqlProvider" />
    <add name="SqlProvider"
      type="System.Web.Security.SqlMembershipProvider"
      connectionStringName="SqlServices"
      enablePasswordRetrieval="false"
      enablePasswordReset="true"
      requiresQuestionAndAnswer="true"
      passwordFormat="Hashed"
      applicationName="/" />
    <add name="AdProvider"
      type="System.Web.Security.ActiveDirectoryMembershipProvider" />
  </providers>
</membership>

When you add a Login control somewhere on a page, then that will use SqlProvider.

You can use a different provider in one of two ways:

  1. You can specify a different provider using MembershipProvider property of the Login control
  2. You could supply a method to be called on the Login controls Authenticate event, where you can pick your provider.

An example of method 2 is:

private void OnAuthenticate(object sender, AuthenticateEventArgs e){
  bool authenticated = false;

  MembershipProvider sqlProvider = Membership.Providers["SqlProvider"];

  authenticated = sqlProvider.ValidateUser(Login1.UserName, Login1.Password);

  if (!authenticated){
    // User not found in database, try Active Directory:
    MembershipProvider adProvider = Membership.Providers["AdProvider"];
    authenticated = adProvider.ValidateUser(Login1.UserName, Login1.Password);
  }

  e.Authenticated = authenticated;
}

Other ways you could do this on your own control is to expose a Provider property, and check to see if that has a value, and use that provider instead of the defaultProvider.

Generally, the provider model has been used to provide a default set of behaviour to a known class - so the Membership providers all have (for example) a method GetUser that return a MembershipUser - the whole purpose of the AspNetSqlMembershipProvider's implementation of GetUser is to load the MembershipUser data from the ASP.NET SQL database - you can see this in action in the Sample Membership Provider

I hope that helps!

Zhaph - Ben Duguid