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ラムダ式を介して

あなたの応答を待っています

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 に答える