セキュリティで保護する必要がある IIS 7.5 で実行されている、インターネットに公開された WCF サービスがあります。HTTP 応答の「サーバー」ヘッダーを削除したいと考えています。
次のコードで IDispatchMessageInspector を実装しました。
public void BeforeSendReply(ref Message reply, object correlationState)
{
var context = WebOperationContext.Current;
if (context != null)
{
context.OutgoingResponse.Headers.Remove(
HttpResponseHeader.Server);
}
}
ただし、サーバー ヘッダーはまだ応答に含まれています。デバッグ時に、 にOutgoingResponse.Headers
が含まれていないHttpResonseHead.Server
ことがわかります。独自の値を記述すると、IIS パイプラインのさらに下にある何かによって明らかに上書きされています。
編集 1
以下を試してみましたが、どちらも良くありません
public class SecureServerHeaderModule : IHttpModule
{
#region Implementation of IHttpModule
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += OnPreSendRequestHeaders;
}
public void Dispose() { }
#endregion
private static void OnPreSendRequestHeaders(object sender, EventArgs e)
{
var context = HttpContext.Current;
if (context != null)
{
context.Response.Headers.Remove("Server");
}
}
}
<system.web>
<httpModules>
<add "snip" />
</httpModlules>
</system.web>
<system.webServer>
<modules>
<add "snip" />
</modlules>
</system.webServer>
編集 2
また、動作しませんでした。
public void BeforeSendReply(ref Message reply, object correlationState)
{
var context = OperationContext.Current;
if (context != null)
{
context.OutgoingMessageProperties.Remove(
HttpResponseHeader.Server.ToString());
context.OutgoingMessageProperties.Add(
HttpResponseHeader.CacheControl.ToString(), "no-store");
}
}