2

我正在尝试调试我不断收到的 System.Web.HttpException。它可能与我正在尝试实现的 BaiscAuthentication 自定义 HttpModule 有关。

BasicAuthentication HttpModule 订阅了管道中的两个事件,BeginRequest 和 AuthenticateRequest。

订阅 BeginRequest 事件的所有代码都成功执行。但是在执行订阅 AuthenticateRequest 的代码之前,我得到了一个 System.Web.HttpException。

异常如下

Exception Details: System.Web.HttpException: This server variable cannot be modified during request execution.

堆栈跟踪如下

[HttpException (0x80004005): This server variable cannot be modified during request execution.]
System.Web.HttpServerVarsCollection.SetServerVariableManagedOnly(String name, String value) +2423129
System.Web.HttpServerVarsCollection.SynchronizeServerVariable(String name, String value) +28
System.Web.HttpRequest.SynchronizeServerVariable(String name, String value) +112
System.Web.Hosting.IIS7WorkerRequest.GetServerVarChanges(HttpContext ctx) +372
System.Web.Hosting.IIS7WorkerRequest.SynchronizeVariables(HttpContext context) +8743312
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +154

该代码在我的本地机器上运行良好,但在我的主机服务器上运行良好。

更新

我找到了有问题的代码,但不知道如何修复这个错误。这是基本代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;


namespace CustomHttpModule
{    
public class CustomModule : IHttpModule 
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
        context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);
    }

    public void context_BeginRequest(Object sender, EventArgs e)
    {
        HttpApplication context = (HttpApplication)sender;
        string authHeader = context.Context.Request.Headers["Authorization"];

            if (String.IsNullOrEmpty(authHeader))
            {
                SendAuthHeader(context);
            }

        context.Context.Response.Clear();
        context.Context.Response.Write("Have reached the end of BeginRequest");
        //context.Context.Response.End();   
    }

    private void SendAuthHeader(HttpApplication context)
    {
        context.Response.Clear();
        context.Response.StatusCode = 401;
        context.Response.StatusDescription = "Authorization Request";
        context.Response.AddHeader("WWW-Authenticate", "Basic realm=\"Secure Area\"");
        context.Response.Write("401 baby, please authenticate");
        context.Response.End();
    }

    public void context_AuthenticateRequest(Object sender, EventArgs e)
    {
        HttpApplication context = (HttpApplication)sender;

        context.Context.Response.Clear();
        context.Context.Response.Write("Have reached the Beginning of AuthenticateRequest");
        context.Context.Response.End();
    }

    public void Dispose()
    {
    }
}
}

此代码产生错误。但是如果你换行..

string authHeader = context.Context.Request.Headers["Authorization"];

string authHeader = context.Context.Request.Headers["Host"];

然后代码也执行得很好。

似乎正在访问Headers["Authorization"];导致该错误的原因。

但是,如果您离开Headers["Authorization"];并取消注释该行//context.Context.Response.End();,那么代码也可以正常执行。

该错误似乎发生在 BeginRequest 结束和 AuthenticateRequest 开始之间。但似乎与代码有关Headers["Authorization"];

我不知道为什么会这样。我想知道这是否只是服务器的一个错误,因为代码在我的本地机器上运行良好。

4

1 回答 1

3

看起来该错误是由AUTH_USERiis7 及更高版本中的集成管道不允许更改的服务器变量引起的。

http://support.microsoft.com/kb/2605401/en-us?sd=rss&spid=14855

此链接提供详细信息。

它说可以在托管模块中处理身份验证通知。但是我仍然不知道如何实现这一点。但是我将就该主题提出一个新问题,因为这个问题变得相当冗长并且已得到有效解决。

于 2011-10-21T16:48:56.073 回答