2009-07-28 67 views
0

我有一個asp.net web應用程序使用我編寫的MembershipProvider和RolesProvider來使用我們的eDirectory ldap服務器。在一個地方引用Web.Config設置

這裏是我的供應商現在:

<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),並參考該部分的提供者配置?

回答

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 
    } 

,現在我在一個地方存儲這些配置設置。

0

如果我理解正確的,你這就是你問什麼:

<membership configSource="membership.config"> 
相關問題