Search code examples
asp.net.netweb-configconfiguration-fileselmah

Config section 'system.web/httpModules' already defined. Sections must only appear once per config file. See the help topic <location> for exceptions


I am aware that there are questions similar to this issue.I am working on a .Net project and it was working fine until yesterday. Today I get to see an error in my config file.

Error I got:

'Config section 'system.web/httpModules' already defined. Sections must only appear once per config file. See the help topic for exceptions'

Below is my config file:

I am not sure which HttpModule has to be removed :(

<configuration>
    <configSections>
        <sectionGroup name="elmah">
            <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
            <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
            <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
            <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
        </sectionGroup>
    </configSections>
    <appSettings>
        <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
        <add key="Internal" value="localhost;localhost2" />
        <add key="External" value="localhost33" />
    </appSettings>
    <system.web.extensions>
        <scripting>
            <webServices>
                <jsonSerialization maxJsonLength="50000000" />
            </webServices>
        </scripting>
    </system.web.extensions>
    <connectionStrings></connectionStrings>
    <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
        <!--Fix for placing secure flag on session cookie-->
        <!--<httpCookies requireSSL="true"/>-->
        <!--<roleManager enabled="true" />-->
        <authentication mode="Forms">
            <!--<forms loginUrl="login.aspx" timeout="9880" requireSSL="true" />-->
            <forms loginUrl="login.aspx" timeout="9880" />
        </authentication>
        <authorization>
            <deny users="?" />
        </authorization>
        <membership defaultProvider="ProjectNameProvider">
            <providers>
                <clear />
                <add name="ProjectNameProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=***" connectionStringName="****" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="***" requiresUniqueEmail="true" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
            </providers>
        </membership>
        <roleManager enabled="true" defaultProvider="ProjectNameProvider">
            <providers>
                <clear />
                <add name="ProjectNameProvider" connectionStringName="***" applicationName="***" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=***" />
            </providers>
        </roleManager>
        <pages>
            <controls>
                <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
                <!--<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />-->
            </controls>
        </pages>
        <machineKey decryptionKey="AutoGenerate,IsolateApps" validationKey="AutoGenerate,IsolateApps" />
        <httpModules>
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
            <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
            <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
        </httpModules>
    </system.web>
    <location path="Login.aspx">
        <system.web>
            <authorization>
                <allow users="*" />
            </authorization>
        </system.web>
    </location>
    <!-- FORGOT SECURITY -->
    <location path="Service/General.asmx">
        <system.web>
            <authorization>
                <allow users="*" />
            </authorization>
        </system.web>
    </location>
    <location path="Accounts_Internal/New_Accounts.aspx">
        <system.web>
            <authorization>
                <allow users="*" />
            </authorization>
        </system.web>
    </location>
    <location path="Forgot/Forgot.aspx">
        <system.web>
            <authorization>
                <allow users="*" />
            </authorization>
        </system.web>
    </location>
    <location path="Forgot/Forgot_Reset.aspx">
        <system.web>
            <authorization>
                <allow users="*" />
            </authorization>
        </system.web>
    </location>
    <location path="Forgot/Forgot_Expired.aspx">
        <system.web>
            <authorization>
                <allow users="*" />
            </authorization>
        </system.web>
    </location>
    <system.webServer>
        <defaultDocument>
            <files>
                <remove value="Login.aspx" />
                <add value="Login.aspx" />
            </files>
        </defaultDocument>
        <modules>
            <remove name="Elmah.ErrorLog" />
            <remove name="Elmah.ErrorMail" />
            <remove name="Elmah.ErrorFilter" />
            <add name="Elmah.ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
            <add name="Elmah.ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
            <add name="Elmah.ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
            <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
            <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
        </modules>
        <!--
        <rewrite><rules><rule name="Add WWW" enabled="true" stopProcessing="true"><match url="^(.*)$" /><conditions><add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" /></conditions><action type="Redirect" url="https://www.{C:0}{PATH_INFO}" redirectType="Permanent" /></rule></rules></rewrite>
    -->
        <httpProtocol>
            <customHeaders>
                <remove name="X-Frame-Options" />
                <add name="X-Frame-Options" value="DENY" />
                <!--<add name="Strict-Transport-Security" value="max-age=31536000"/>-->
            </customHeaders>
        </httpProtocol>
        <validation validateIntegratedModeConfiguration="false" />
    </system.webServer>
    <elmah>
        <errorLog applicationName="***" type="Elmah.SqlErrorLog, Elmah" connectionStringName="SQLErrorConnection" />
        <errorMail />
        <!--
            See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
            more information on remote access and securing ELMAH.
        -->
        <security allowRemoteAccess="false" />
    </elmah>
    <location>
        <system.web>
            <customErrors mode="Off" />
            <httpModules>
                <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
                <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
                <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
            </httpModules>
        </system.web>
    </location>
    <runtime>
        <assemblyBinding
            xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="HtmlAgilityPack" publicKeyToken="bd319b19eaf3b43a" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-1.8.6.0" newVersion="1.8.6.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
    <location path="elmah.axd" inheritInChildApplications="false">
        <system.web>
            <httpHandlers>
                <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
            </httpHandlers>
            <!-- 
            See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
            more information on using ASP.NET authorization securing ELMAH.

          <authorization><allow roles="admin" /><deny users="*" /></authorization>
          -->
        </system.web>
        <system.webServer>
            <handlers>
                <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
            </handlers>
        </system.webServer>
    </location>
</configuration>

Solution

  • You are adding all of the ELMAH modules twice:

    <add name="Elmah.ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
    <add name="Elmah.ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
    <add name="Elmah.ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
    <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
    <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
    <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
    

    I've described how to install ELMAH manually here: Configure elmah.io manually. The guide is for elmah.io (a commercial service that uses ELMAH), but the approach is the same for your code. The only thing you need to replace from my sample is ELMAH's ErrorLog-element.