取得し続ける System.Web.HttpException をデバッグしようとしています。これは、実装しようとしている BaiscAuthentication カスタム HttpModule に関連している可能性があります。
BasicAuthentication HttpModule は、パイプラインの 2 つのイベント、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"];
なぜこれが必要なのかわかりません。ローカル マシンでコードが正常に動作するため、サーバーの単なるバグではないかと考えています。