在一个位置引用Web.Config设置

时间:2009-07-28 19:20:00

标签: .net asp.net web-config

我有一个asp.net Web应用程序正在使用我编写的MembershipProvider和RolesProvider来使用我们的eDirectory ldap服务器。

以下是我的提供商:

<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>

这两个提供程序在web.config中配置,两者的设置相同。有没有办法将设置存储在另一个部分(最好是appsettings)并引用该部分以供提供商配置?

2 个答案:

答案 0 :(得分:0)

如果我理解正确,那就是你所要求的:

<membership configSource="membership.config">

答案 1 :(得分:0)

我放了一些我很满意的地方:

我创建了这个配置类:

    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
        ...
    }

然后我注册了一个新的配置部分并将配置存储在ldapConfiguration元素中:

    <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>

然后在我的每个提供者类中,我使用我的新配置对象初始化它们:

    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
    }

现在我将这些配置设置存储在一个位置。