将asp.net会员特定设置移动到单独的配置文件

时间:2011-04-29 15:50:00

标签: c# asp.net

是否可以将asp.net成员资格/角色设置移动到单独的配置文件中。这对自定义成员资格提供程序很有用。

<authentication  mode="Forms">
  <forms loginUrl="~/Login.aspx" timeout="144600" slidingExpiration="true">
</authentication>
<membership defaultProvider="TestMembership">
   <providers>
      <add name="TestMembership" type="Test.Membership.TestMembership">
   </providers>
</membership>
<roleManager enabled="true" defaultProvider="TestRole"
             cacheRolesInCookie="true" cookieName=".Test" cookieTimeout="1440"
             cookiePath="/">
   <providers>
      <add name="TestRole" type="Test.Membership.TestRole" />
   </providers>
</roleManager>

1 个答案:

答案 0 :(得分:8)

任何.NET配置部分都可以外部化 - 试试这个:

<authentication configSource="auth.config" />
<membership configSource="members.config" />
<roleManager configSource="roles.config" />

或您想要使用的任何名称。

单个文件需要包含您已外部化的整个部分:

<强> auth.config

<?xml version="1.0" encoding="utf-8"?>
<authentication  mode="Forms">
  <forms loginUrl="~/Login.aspx" timeout="144600" slidingExpiration="true">
</authentication>

<强> members.config

<?xml version="1.0" encoding="utf-8"?>
<membership defaultProvider="TestMembership">
   <providers>
      <add name="TestMembership" type="Test.Membership.TestMembership">
   </providers>
</membership>

<强> roles.config

<?xml version="1.0" encoding="utf-8"?>
<roleManager enabled="true" defaultProvider="TestRole"
             cacheRolesInCookie="true" cookieName=".Test" cookieTimeout="1440"
             cookiePath="/">
   <providers>
      <add name="TestRole" type="Test.Membership.TestRole" />
   </providers>
</roleManager>