我有一个在 IIS 7.5 上运行的暴露于互联网的 WCF 服务,我需要保护它的安全。我想删除 HTTP 响应中的“Server”标头。
我已经实现了 IDispatchMessageInspector,代码如下。
public void BeforeSendReply(ref Message reply, object correlationState)
{
var context = WebOperationContext.Current;
if (context != null)
{
context.OutgoingResponse.Headers.Remove(
HttpResponseHeader.Server);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,服务器标头仍在响应中。在调试时,我可以看到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>
Run Code Online (Sandbox Code Playgroud)
编辑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");
}
}
Run Code Online (Sandbox Code Playgroud)
这可以使用一个IDispatchMessageInspector
public class SecureBehaviour : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request,
IClientChannel channel, InstanceContext instanceContext)
{
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
var httpCtx = HttpContext.Current;
if (httpCtx != null)
{
httpCtx.Response.Headers.Remove(
HttpResponseHeader.Server.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5161 次 |
| 最近记录: |