Dai
Dai

Reputation: 155055

IIS 8 - Static file requests going through managed pipeline in spite of runAllManagedModulesForAllRequests="false"

I have an ASP.NET MVC 2 Web Application that works fine in my local Cassini server and it also worked fine when I last tested it against IIS 7.5 a few months ago.

My web.config looks like this (irrelevant sections removed):

<?xml version="1.0"?>
<configuration>

    <system.web>

        <authentication mode="Forms">
            <!-- 2880 minutes is 48 hours -->
            <forms loginUrl="~/Welcome/Login" timeout="2880" slidingExpiration="true" />
        </authentication>

        <customErrors mode="RemoteOnly" />

        <httpHandlers>
            <remove verb="*"     path="*.asmx" />
            <add verb="*"        path="*.asmx"             validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add verb="*"        path="*_AppService.axd"   validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        </httpHandlers>

        <httpModules>
            <add name="ScriptModule"     type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        </httpModules>

    </system.web>

    <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules runAllManagedModulesForAllRequests="true">
            <remove name="ScriptModule"/>
            <remove name="UrlRoutingModule"/>
            <add name="ScriptModule"     preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="UrlRoutingModule"                               type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </modules>
        <handlers>
            <remove name="WebServiceHandlerFactory-Integrated"/>
            <remove name="ScriptHandlerFactory"/>
            <remove name="ScriptHandlerFactoryAppServices"/>
            <remove name="ScriptResource"/>
            <remove name="UrlRoutingHandler"/>
            <add name="ScriptHandlerFactory"            preCondition="integratedMode" verb="*"        path="*.asmx"             type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ScriptHandlerFactoryAppServices" preCondition="integratedMode" verb="*"        path="*_AppService.axd"   type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ScriptResource"                  preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="UrlRoutingHandler"               preCondition="integratedMode" verb="*"        path="UrlRouting.axd"     type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </handlers>
        <httpErrors>
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" prefixLanguageFilePath="" path="Views\Shared\Iis404.htm" responseMode="File" />
        </httpErrors>

    </system.webServer>

</configuration>

I'm now deploying it to IIS 8 on an Azure VM and requests for static content, e.g. /Styles/style.css go through the managed request pipeline, and because I have an authentication system in-place it means that those requests get blocked and replaced with my application's login screen.

I found this QA ( Prevent IIS from serving static files through ASP.NET pipeline ) and I changed <modules runAllManagedModulesForAllRequests="true"> to <modules runAllManagedModulesForAllRequests="false">, however that didn't fix it: requests to /Styles/style.css still caused my ASP.NET MVC code to be invoked (it invokes the Login action on my WelcomeController class, which fails because now the Controller.User property returns null).

I'm not aware of any changes in IIS8 that would trigger this - but how can I fix it anyway?

Update:

I added this to my Global.asax.cs but it didn't work:

routes.IgnoreRoute("Scripts/{*pathInfo}");
routes.IgnoreRoute("Styles/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");

Update: I've installed IIS 8 on my Windows 8 Enterprise laptop and to my surprise, it works as-intended on there - so there's some configuration difference between my Windows 8 laptop and the Server 2012 Azure VM. Hmmm.

Upvotes: 1

Views: 1527

Answers (1)

Dai
Dai

Reputation: 155055

I began looking for differences between the IIS configurations of my local machine and the Azure VM and I found the solution:

I had overlooked the IIS > {website} > Authentication > Anonymous configuration: on the Azure VM it was set to Specific user for a user that didn't exist! Whereas in my local IIS it was set to Application pool identity.

I changed it to Application pool identity and now it works as-intended.

I'm surprised IIS didn't give me any other error messages or a more helpful way to diagnose the problem. Hmpf.

Upvotes: 2

Related Questions