1

让我们考虑以下 web.config 文件

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <connectionStrings>

  </connectionStrings>
  <system.web>
    <pages theme="PetShop" styleSheetTheme="PetShop" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
      <controls>
        <add tagPrefix="blt" namespace="BLToolkit.Web.UI" assembly="BLToolkit.4" />
      </controls>
    </pages>
    <!--
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        -->
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      </assemblies>
    </compilation>
    <!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
         -->
    <authentication mode="Forms">
      <forms name="PetShopAuth" loginUrl="SignIn.aspx" protection="None" timeout="60" />
    </authentication>
    <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.
        -->
    <customErrors defaultRedirect="Error.aspx" mode="RemoteOnly" />
    <sessionState mode="Off" />
    <anonymousIdentification enabled="true" />
    <profile automaticSaveEnabled="false" defaultProvider="ShoppingCartProvider">
      <providers>
        <add name="ShoppingCartProvider" connectionStringName="ProfileDB" type="PetShop.BusinessLogic.ProfileProvider" applicationName=".NET Pet Shop 4.0" />
        <add name="WishListProvider" connectionStringName="ProfileDB" type="PetShop.BusinessLogic.ProfileProvider" applicationName=".NET Pet Shop 4.0" />
        <add name="AccountInfoProvider" connectionStringName="ProfileDB" type="PetShop.BusinessLogic.ProfileProvider" applicationName=".NET Pet Shop 4.0" />
      </providers>
      <properties>
        <add name="ShoppingCart" type="PetShop.BusinessLogic.Cart" allowAnonymous="true" provider="ShoppingCartProvider" />
        <add name="WishList" type="PetShop.BusinessLogic.Cart" allowAnonymous="true" provider="WishListProvider" />
        <add name="AccountInfo" type="PetShop.ObjectModel.Address" allowAnonymous="false" provider="AccountInfoProvider" />
      </properties>
    </profile>
    <!-- Membership Provider for SqlServer -->
    <membership defaultProvider="SQLMembershipProvider">
      <providers>
        <add name="SQLMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="MembershipDB" applicationName=".NET Pet Shop 4.0" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" />
      </providers>
    </membership>
    <caching>
      <sqlCacheDependency enabled="true" pollTime="10000">
        <databases>
          <add name="MSPetShop4" connectionStringName="SQLConnString1" pollTime="10000" />
        </databases>
      </sqlCacheDependency>
    </caching>
  </system.web>
  <location path="UserProfile.aspx">
    <system.web>
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
  </location>
  <location path="CheckOut.aspx">
    <system.web>
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
  </location>
</configuration>

通过阅读上述配置文件,我需要更改成员提供程序节点中类型字段的属性值。

    type="System.Web.Security.SqlMembershipProvider" 

到新的价值

type="sample.SqlMembershipProvider"

通过 c#.net lambda 表达式

等待您的回复

4

1 回答 1

1

我完成了解决方案

  var xDoc = XDocument.Load(inputPathToConfigFile);
                var ns = xDoc.Descendants().First(x => x.Name.LocalName == "configuration").Name.Namespace;

                var prop = xDoc.Descendants(ns + "membership")
                .First(p => p.Attribute("defaultProvider").Value == "SQLMembershipProvider");

               if(prop.HasAttributes)
               {
                   var prop1 = prop.Descendants(ns + "add").First(p => p.Attribute("type").Value == "System.Web.Security.SqlMembershipProvider");
                   prop1.Attribute("type").Value = "sample.membershipprovider";
                   xDoc.Save(inputPathToConfigFile);

               }

仅供参考...

于 2012-11-07T06:11:19.820 回答