当社のセキュリティ チームでは、すべての Cookie を Secure=true に設定する必要があります。
MVC AntiForgery のセキュア プロパティを設定するには、次のコードを使用します。
protected void Application_BeginRequest(object sender, EventArgs e)
{
AntiForgeryConfig.RequireSsl = HttpContext.Current.Request.IsSecureConnection;
}
しかし、SSL を使用していないテスト サーバーで問題が発生しました。時々、自然発生的なエラーが発生します
The anti-forgery system has the configuration value AntiForgeryConfig.RequireSsl = true, but the current request is not an SSL request.
例外の場所を特定するために ASP.NET MVC コードを調べたところ、次のことがわかりました。
private void CheckSSLConfig(HttpContextBase httpContext)
{
if (_config.RequireSSL && !httpContext.Request.IsSecureConnection)
{
throw new InvalidOperationException(WebPageResources.AntiForgeryWorker_RequireSSL);
}
}
実行シーケンスが
AntiForgeryConfig.RequireSsl = HttpContext.Current.Request.IsSecureConnection;
// ... something happens in between
if (_config.RequireSSL && !httpContext.Request.IsSecureConnection)
{
throw new InvalidOperationException(WebPageResources.AntiForgeryWorker_RequireSSL);
}
しかし、一部のリクエストでは、テスト サーバーで SSL を使用していないにもかかわらず、HttpContext.Current.Request.IsSecureConnection が true を返しているようです。
何が起こっているのですか?なぜこの例外が発生するのですか?