简体   繁体   English

HttpModule EndRequest处理程序调用两次

[英]HttpModule EndRequest handler called twice

I am attempting to implement authentication for a REST service implemented in WCF and hosted on Azure. 我正在尝试为在WCF中实现并托管在Azure上的REST服务实现身份验证。 I am using HttpModule to handle the AuthenticationRequest, PostAuthenticationRequest and EndRequest events. 我正在使用HttpModule来处理AuthenticationRequest,PostAuthenticationRequest和EndRequest事件。 If the Authorization header is missing or if the token contained therein is invalid, during EndRequest I am setting the StatusCode on the Response to 401. However, I have determined that EndRequest is called twice, and on the second call the response has already had headers set, causing the code which sets the StatusCode to throw an exception. 如果Authorization标头丢失或者其中包含的标记无效,则在EndRequest期间我将响应的StatusCode设置为401.但是,我已确定调用EndRequest两次,并且在第二次调用时响应已经有标题set,导致设置StatusCode的代码抛出异常。

I added locks to Init() to ensure that the handler wasn't being registered twice; 我向Init()添加了锁,以确保处理程序没有被注册两次; still ran twice. 还是跑了两次。 Init() also ran twice, indicating that two instances of the HttpModule were being created. Init()也运行了两次,表明正在创建两个HttpModule实例。 However, using Set Object ID in the VS debugger seems to indicate that the requests are actually different requests. 但是,在VS调试器中使用Set Object ID似乎表明请求实际上是不同的请求。 I've verified in Fiddler that there is only one request being issued to my service from the browser. 我在Fiddler中验证过,浏览器只向我的服务发出了一个请求。

If I switch to using global.asax routing instead of depending on the WCF service host configuration, the handler is only called once and everything works fine. 如果我切换到使用global.asax路由而不是依赖于WCF服务主机配置,则只调用一次处理程序,一切正常。

If I add configuration to the system.web configuration section as well as the system.webServer configuration section in Web.config, the handler is only called once and everything works fine. 如果我将配置添加到system.web配置部分以及Web.config中的system.webServer配置部分,则只调用一次处理程序,一切正常。

So I have mitigations, but I really dislike behavior I don't understand. 所以我有缓解措施,但我真的不喜欢我不明白的行为。 Why does the handler get called twice? 为什么处理程序被调用两次?

Here is a minimal repro of the problem: 这是问题的最小重复:

Web.config: Web.config文件:

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <!--<httpModules>
      <add name="AuthModule" type="TestWCFRole.AuthModule, TestWCFRole"/>
    </httpModules>-->
  </system.web>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
    <services>
      <service name="TestWCFRole.Service1">
        <endpoint binding="webHttpBinding" name="RestEndpoint" contract="TestWCFRole.IService1" bindingConfiguration="HttpSecurityBinding" behaviorConfiguration="WebBehavior"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
    <bindings>
      <webHttpBinding>
        <binding name="HttpSecurityBinding" >
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="AuthModule" type="TestWCFRole.AuthModule, TestWCFRole"/>
    </modules>
    <directoryBrowse enabled="true"/>
  </system.webServer>

Http module: Http模块:

using System;
using System.Web;

namespace TestWCFRole
{
    public class AuthModule : IHttpModule
    {
        /// <summary>
        /// You will need to configure this module in the web.config file of your
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule Members

        public void Dispose()
        {
            //clean-up code here.
        }

        public void Init(HttpApplication context)
        {
            // Below is an example of how you can handle LogRequest event and provide 
            // custom logging implementation for it
            context.EndRequest += new EventHandler(OnEndRequest);
        }

        #endregion

        public void OnEndRequest(Object source, EventArgs e)
        {
            HttpContext.Current.Response.StatusCode = 401;
        }
    }
}

When an ASP.net application starts up, to maximize performance the ASP.NET Worker process will instantiate as many HttpApplication objects as it needs. 当ASP.net应用程序启动时,为了最大限度地提高性能,ASP.NET Worker进程将根据需要实例化多个HttpApplication对象。 Each HttpApplication object, will also instantiate one copy of each IHttpModule that is registered and call the Init method! 每个HttpApplication对象还将实例化已注册的每个IHttpModule一个副本并调用Init方法! That's really an internal design of the ASP.NET process running under IIS (or cassini which is VS built in webserver). 这实际上是在IIS下运行的ASP.NET进程的内部设计(或cassini,它是在Web服务器中构建的VS)。 Might be because your ASPX page has links to other resources which your browser will try to download, an external resource, and iframe, a css file, or maybe ASP.NET Worker Process behavior. 可能是因为您的ASPX页面链接到浏览器将尝试下载的其他资源,外部资源,iframe,css文件或ASP.NET Worker Process行为。

Luckily it's not the case for Global.asax: 幸运的是,Global.asax的情况并非如此:

Here's from MSDN : 这是来自MSDN

The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. Application_Start和Application_End方法是不代表HttpApplication事件的特殊方法。 ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance. ASP.NET在应用程序域的生命周期内调用它们一次,而不是为每个HttpApplication实例调用它们。

However HTTPModule's init method is called once for every instance of the HttpApplication class after all modules have been created 但是,在创建所有模块之后,为HttpApplication类的每个实例调用一次HTTPModule's init方法

The first time an ASP.NET page or process is requested in an application, a new instance of HttpApplication is created. 第一次在应用程序中请求ASP.NET页面或进程时,会创建一个新的HttpApplication实例。 However, to maximize performance, HttpApplication instances might be reused for multiple requests. 但是,为了最大限度地提高性能,HttpApplication实例可能会被重用于多个请求。

And illustrated by the following diagram: 并通过下图说明: 在此输入图像描述

If you want code that's guaranteed to run just once, you can either use Application_Start of the Global.asax or set a flag and lock it in the underlying module which is don't think is a good practice for the sake of Authentication! 如果您希望保证只运行一次的代码,您可以使用Global.asax Application_Start或设置一个标志并将其锁定在底层模块中,这对于身份验证来说并不是一个好习惯!

Sorry no clue to why it could be called twice, however EndRequest can end up being called for multiple reasons. 对不起,为什么它可以被调用两次没有任何线索,但EndRequest最终可能因多种原因被调用。 request finished, request was aborted, some error happened. 请求完成,请求被中止,发生了一些错误。 So i wouldn't put my trust in assuming that if you get there, you actually have a 401, it could be for other reasons. 因此,我不会相信假设如果你到达那里,你实际上有401,这可能是出于其他原因。

I'd just keep my logic in the AuthenticateRequest pipeline: 我只是将我的逻辑保留在AuthenticateRequest管道中:

    public class AuthenticationModule : IHttpModule
    {
        public void Dispose() { }

        public void Init(HttpApplication context)
        {
            context.AuthenticateRequest += Authenticate;
        }

        public static void Authenticate(object sender, EventArgs e)
        {
            // authentication logic here            
            //.............

            if (authenticated) {
                HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(myUser, myRoles);
            }

            // failure logic here           
            //.............         
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM