简体   繁体   中英

Referencing Web.Config Settings in one place

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?

如果我正确理解了您的要求,那就是:

<membership configSource="membership.config">

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM