Migrating legacy ASP.NET to MVC 2 (RC): HttpApplication events not firing, User principal is null

被刻印的时光 ゝ 提交于 2019-11-29 15:23:59

UPDATED Alright, I think the issue is a failure in my understanding of how MVC really works (which was not clear until I pulled back and with a fresh mind compared the differences between ASP.NET and MVC web configs).

The reason for my problem is because the following setting is required for MVC applications to run properly in integrated mode (the important part is runAllManagedModulesForAllRequests="true"):

<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"/>

       <!-- omitted for clarify -->
    </modules>
    <handlers>
        <!-- omitted for clarity -->
        <add name="MvcHttpHandler" preCondition="integratedMode" verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=2.0.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>
</system.webServer>

Where this causes an issue for me would be the fact that my ASP.NET application uses a default authorization rule denying access to unauthenticated users:

  <system.web>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>

Whereas MVC does not encourage this practice, but rather using AuthorizeAttribute or some other filter.

Until I move all of my existing code over to MVC (which will take quite some time), I will have to come up with a clever solution for keeping the ASP.NET-style authorization in place to push out unauthenticated requests (maybe a custom HttpModule?) for secured resources (which for us is everything except images, javascript, css, static html, and the login/logout pages), and remove the above from the web.config.

Furthermore, my application, being originally just ASP.NET, assumed that Application_BeginRequest (and the likes) would only be significant resource requests, like ASPX, ASMX, ASHX, and AXD pages. So, I need to adjust any application events to stop any resource intensive processing (security checks that hit the DB, etc.) for static resources that I don't care about (again, things like images, etc.).

SUMMARY

runAllManagedModulesForAllRequests="true" is required for MVC in integrated mode. If your ASP.NET application uses location-based authorization heavily, and/or does a lot of processing on request lifecycle events, you're going to have some extra work cut out for you to get MVC working along side your ASP.NET forms.

No Refunds No Returns

This may or may not help: I just finished reviewing the Nerd Dinner code and theycovered this topic specifically in the area of unit testing. Try browsering this http://www.wrox.com/WileyCDA/Section/id-321793.html and scrolling nearly to the bottom where they cover the sample unit tests. They have some fake out magic that makes their sample Account controller work to let you supply a fake user ID. Might work for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!