私が取得し続けるSystem.Web.HttpExceptionをデバッグしようとしています。実装しようとしているBaiscAuthenticationカスタムHttpModuleに関連する可能性があります。System.Web.HttpException(BasicAuthenticationカスタム)HttpModule
BasicAuthentication HttpModuleは、BeginRequestとAuthenticateRequestという2つのイベントにサブスクライブしています。
BeginRequestイベントを購読しているすべてのコードが正常に実行されます。しかし、AuthenticateRequestを購読するコードが実行される前に、私はSystem.Web.HttpExceptionを取得します。
Exception Details: System.Web.HttpException: This server variable cannot be modified during request execution.
を次のようにExeceptionがあると
[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
を次のようにスタックトレースがあるコードは私のローカルマシン上ではなく、私のホストサーバー上で正常に動作。
UPDATE
私は、問題のあるコードを見つけましたが、バグを修正する方法を考え出したていません。ここに基本コードがあります。
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["Host"];
に行...
string authHeader = context.Context.Request.Headers["Authorization"];
を変更した場合しかし、その後のコードも細かい実行されます。
バグの原因となっているのはHeaders["Authorization"];
にアクセスしているようです。
ただし、Headers["Authorization"];
のままにして、//context.Context.Response.End();
という行のコメントを解除すると、コードも正常に実行されます。
BeginRequestの終了とAuthenticateRequestの開始の間にバグが発生しているようです。しかし、コードに関連しているようです。Headers["Authorization"];
私はこれがなぜ必要なのか分かりません。コードがローカルマシン上でうまく動作するので、サーバにバグがあるかどうか疑問に思っています。