views:

117

answers:

2

I have an asp.net web application that is using a MembershipProvider and RolesProvider that I wrote to use our eDirectory ldap servers.

Here are my providers now:

<membership defaultProvider="EDirectoryMembershipProvider">
  <providers>
    <clear/>
    <add name="EDirectoryMembershipProvider" type="EDirectoryMembershipProvider" 
         PrimaryLdapServer="1.2.3.4" 
         SecondaryLdapServer="5.6.7.8" 
         LdapPort="1234" 
         CertPath="d:\mycert.crt" />
  </providers>
</membership>

<roleManager enabled="true" defaultProvider="EDirectoryRoleProvider" cacheRolesInCookie="true" cookieRequireSSL="true">
  <providers>
    <clear/>
    <add name="EDirectoryRoleProvider" type="EDirectoryRoleProvider" 
         PrimaryLdapServer="1.2.3.4" 
         SecondaryLdapServer="5.6.7.8" 
         LdapPort="1234" 
         CertPath="d:\mycert.crt" />
  </providers>
</roleManager>

These two providers are configured in web.config and the settings for both are the same. Is there a way to store the settings in another section (preferably appsettings) and reference that section for the providers' configuration?

A: 

If I understood you correctly that's what you asking for:

<membership configSource="membership.config">
Jacob
A: 

I put something in place that I'm happy with:

I created this configuration class:

    public class LdapConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("PrimaryServer", IsRequired = true)]
        public string PrimaryServer
        {
            get { return (string)base["PrimaryServer"]; }
            set { base["PrimaryServer"] = value; }
        }

        [ConfigurationProperty("SecondaryServer", IsRequired = true)]
        public string SecondaryServer
        {
            get { return (string)base["SecondaryServer"]; }
            set { base["SecondaryServer"] = value; }
        }

        // more properties that I need
        ...
    }

Then I registered a new config section and stored the configuration in an ldapConfiguration element:

    <configSections>
      ...
      <section name="ldapConfiguration" type="LdapConfiguration"/>
    </configSections>
    <ldapConfiguration PrimaryServer="1.2.3.4" SecondaryServer="5.6.7.8" Port="1234" CertPath="d:\mycert.cert" />

    <system.web>
    ...
    <!--Oh, my God, this is so awesome! In your face!-->
    <membership defaultProvider="EDirectoryMembershipProvider">
      <providers>
        <clear/>
        <add name="EDirectoryMembershipProvider" type="EDirectoryMembershipProvider" />
      </providers>
    </membership>

    <roleManager enabled="true" defaultProvider="GtccLdapRoleProvider" cacheRolesInCookie="true" cookieRequireSSL="true">
       <providers>
          <clear/>
          <add name="GtccLdapRoleProvider" type="EDirectoryRoleProvider" />
       </providers>
    </roleManager>
  ...
  </system.web>

Then in each of my provider classes, I initialized them using my new configuration object:

    public EDirectoryMembershipProvider()
    {
        var ldapConfig = (LdapConfiguration)WebConfigurationManager.OpenWebConfiguration("/").GetSection("ldapConfiguration");
        this.PrimaryLdapServer = ldapConfig.PrimaryServer;
        this.SecondaryLdapServer = ldapConfig.SecondaryServer;
        // initialize the rest here
        ...
    }

    public override void Initialize(string name, NameValueCollection config)
    {
        base.Initialize(name, config);
        // not initializing here anymore
    }

And now I am storing those configuration settings in one spot.

Ronnie Overby