有没有办法从IIS7中删除“服务器”响应头?有一些文章显示使用HttpModules我们可以实现同样的目的。如果我们没有服务器的管理权限,这将非常有用。另外我不想写ISAPI过滤器。
我有我的服务器的管理员权限。所以我不想做上面的事情。所以,请帮我做同样的事。
将其添加到您的global.asax.cs:
protected void Application_PreSendRequestHeaders()
{
Response.Headers.Remove("Server");
Response.Headers.Remove("X-AspNet-Version");
Response.Headers.Remove("X-AspNetMvc-Version");
}
这个web.config
设置可以从ASP.NET响应中删除所有不必要的头文件(至少从IIS 10开始):
<!--Removes version headers from response -->
<httpRuntime enableVersionHeader="false" />
<httpProtocol>
<customHeaders>
<!--Removes X-Powered-By header from response -->
<clear />
</customHeaders>
</httpProtocol>
<security>
<!--Removes Server header from response-->
<requestFiltering removeServerHeader ="true" />
</security>
请注意,这隐藏了“应用程序”的所有标题,所有其他方法也是如此。如果你是访问某些默认页面或由IIS本身或应用程序外部的ASP.NET生成的错误页面,这些规则将不适用。理想情况下,它们应该位于IIS的根级别,并且可能会对IIS本身留下一些错误响应。
附: IIS 10中有一个bug,即使配置正确,它有时也会显示服务器头。它现在应该修复,但必须更新IIS / Windows。
尝试将HKLM\SYSTEM\CurrentControlSet\Services\HTTP\Parameters\DisableServerHeader
注册表项设置为REG_DWORD
的1
。
UrlScan 还可以使用AlternateServerName=
下的[options]
删除服务器头。
关注eddiegroves' answer,取决于URLScan的版本,您可能更喜欢RemoveServerHeader=1
下的[options]
。
我不确定在哪个版本的URLScan中添加了此选项,但它已在2.5及更高版本中提供。
我找到了一篇文章解释了为什么我们需要同时进行注册表编辑并使用UrlScan等工具在IIS中正确设置。我在我们的服务器上关注它,它的工作原理:http://blogs.msdn.com/b/varunm/archive/2013/04/23/remove-unwanted-http-response-headers.aspx。如果您仅使用UrlScan但未进行注册表更改,则在停止World Wide Publishing Service期间,您的服务器将从HTTP.sys文件返回服务器http响应。此外,以下是使用UrlScan工具的常见问题:http://msdn.microsoft.com/en-us/library/ff648552.aspx#ht_urlscan_008
在IIS 10中,我们使用与Drew方法类似的解决方案,即:
using System;
using System.Web;
namespace Common.Web.Modules.Http
{
/// <summary>
/// Sets custom headers in all requests (e.g. "Server" header) or simply remove some.
/// </summary>
public class CustomHeaderModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += OnPreSendRequestHeaders;
}
public void Dispose() { }
/// <summary>
/// Event handler that implements the desired behavior for the PreSendRequestHeaders event,
/// that occurs just before ASP.NET sends HTTP headers to the client.
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void OnPreSendRequestHeaders(object sender, EventArgs e)
{
//HttpContext.Current.Response.Headers.Remove("Server");
HttpContext.Current.Response.Headers.Set("Server", "MyServer");
}
}
}
显然,在项目中添加对该dll的引用,并在所需的配置中添加模块:
<system.webServer>
<modules>
<!--Use http module to remove/customize IIS "Server" header-->
<add name="CustomHeaderModule" type="Common.Web.Modules.Http.CustomHeaderModule" />
</modules>
</system.webServer>
重要说明1:此解决方案需要将应用程序池集合为集成;
重要说明2:Web应用程序中的所有响应都将受此影响(包括css和js);
我在这里和其他几个类似的堆栈溢出线程上尝试了所有的东西。
我挂了一会儿因为忘记在更改配置后清除浏览器缓存。如果您不这样做并且文件位于本地缓存中,它将使用原始标头(duh)将其提供给您。
我得到它主要是通过删除runAllManagedModulesForAllRequests:
<modules runAllManagedModulesForAllRequests="true">
这从大多数静态文件中删除了无关的标题,但我仍然在我的WebAPI项目中的一些静态文件中获得了“服务器”标题。
我终于找到并应用了这个解决方案,现在所有不需要的标题都消失了:
https://www.dionach.com/blog/easily-remove-unwanted-http-headers-in-iis-70-to-85
讨论他的代码在这里:
https://github.com/Dionach/StripHeaders/releases/tag/v1.0.5
这是一个Native-Code模块。它可以删除Server标头,而不只是删除值。默认情况下会删除:
我研究了这个,URLRewrite方法效果很好。似乎无法在任何地方找到更改脚本。我写了这与PowerShell v2及更高版本兼容,并在IIS 7.5上进行了测试。
# Add Allowed Server Variable
Add-WebConfiguration /system.webServer/rewrite/allowedServerVariables -atIndex 0 -value @{name="RESPONSE_SERVER"}
# Rule Name
$ruleName = "Remove Server Response Header"
# Add outbound IIS Rewrite Rule
Add-WebConfigurationProperty -pspath "iis:\" -filter "system.webServer/rewrite/outboundrules" -name "." -value @{name=$ruleName; stopProcessing='False'}
#Set Properties of newly created outbound rule
Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -filter "system.webServer/rewrite/outboundRules/rule[@name='$ruleName']/match" -name "serverVariable" -value "RESPONSE_SERVER"
Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -filter "system.webServer/rewrite/outboundRules/rule[@name='$ruleName']/match" -name "pattern" -value ".*"
Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -filter "system.webServer/rewrite/outboundRules/rule[@name='$ruleName']/action" -name "type" -value "Rewrite"
您可以在Global.asax.cs文件中添加以下代码
protected void Application_PreSendRequestHeaders()
{
Response.Headers.Remove("Server");
}
在IIS7中,您必须使用HTTP模块。在VS中将以下内容构建为类库:
namespace StrongNamespace.HttpModules
{
public class CustomHeaderModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += OnPreSendRequestHeaders;
}
public void Dispose() { }
void OnPreSendRequestHeaders(object sender, EventArgs e)
{
HttpContext.Current.Response.Headers.Set("Server", "Box of Bolts");
}
}
}
然后将以下内容添加到web.config中,或者在IIS中进行配置(如果在IIS中进行配置,则程序集必须位于GAC中)。
<configuration>
<system.webServer>
<modules>
<add name="CustomHeaderModule"
type="StrongNamespace.HttpModules.CustomHeaderModule" />
</modules>
</system.webServer>
</configuration>
启用URL重写模块版本2.0 for IIS(UrlRewrite)后,在配置部分<configuration>
➡<system.webServer>
➡<rewrite>
中添加出站规则:
<outboundRules>
<rule name="Remove RESPONSE_Server" >
<match serverVariable="RESPONSE_Server" pattern=".+" />
<action type="Rewrite" value="" />
</rule>
</outboundRules>
Scott Mitchell在removing unnecessary headers的博客文章中提供了解决方案。
正如在其他答案中已经说过的那样,对于Server
标题,有http module solution或UrlScan模块。 (IIS7.5 +中不再提供URLScan模块。请使用URLRewrite instead for blanking it。)
对于X-AspNet-Version
和X-AspNetMvc-Version
,他提供了一种比在每次响应中删除它们更好的方法:根本就不生成它们。
在web.config中使用enableVersionHeader
禁用X-AspNet-Version
<httpRuntime enableVersionHeader="false" />
在.Net Application_Start事件中使用MvcHandler.DisableMvcResponseHeader
来禁用X-AspNetMvc-Version
MvcHandler.DisableMvcResponseHeader = true;
最后,在IIS配置中删除X-Powered-By
自定义标头。 (这可以在web.config,configuration/system.webServer/httpProtocol/customHeaders/remove[name=X-Powered-By]
中完成)
请注意,如果您有ARR(应用程序请求路由),它还将添加自己的X-Powered-By
,自定义标头设置不会删除它。必须通过IIS管理器上的IIS管理器(不在站点上)上进行编辑器配置来删除这个:转到system.webServer/proxy
节点并将arrResponseHeader
设置为false
。在IISReset
之后,它被考虑在内。
(我发现这个here,除了这篇文章是关于旧IIS 6.0配置事物的方式。)
不要忘记应用程序代码的解决方案默认情况下不适用于在静态内容上生成的标头(您可以激活runAllManagedModulesForAllRequests
来更改它,但它会导致所有请求运行.Net管道)。它不是X-AspNetMvc-Version
的问题,因为它没有添加到静态内容上(至少如果静态请求不在.Net管道中运行)。
旁注:当目标是隐藏使用的技术时,您还应该更改标准.Net cookie名称(.ASPXAUTH
如果表单auth激活(在web.config中使用name
标签上使用forms
属性),ASP.NET_SessionId
(在<sessionState cookieName="yourName" />
下使用web.config中的system.web
)标签),__RequestVerificationToken
(用AntiForgeryConfig.CookieName
代码更改,但遗憾的是不适用于此系统在html中生成的隐藏输入))。
实际上,上面显示的编码模块和Global.asax示例仅适用于有效请求。
例如,在URL的末尾添加<,您将收到一个“错误请求”页面,该页面仍然显示服务器标头。许多开发人员忽视了这一点。
显示的注册表设置也不起作用。 URLScan是删除“服务器”标头的唯一方法(至少在IIS 7.5中)。
或者在web.config中添加:
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="X-AspNet-Version" />
<remove name="X-AspNetMvc-Version" />
<remove name="X-Powered-By" />
<!-- <remove name="Server" /> this one doesn't work -->
</customHeaders>
</httpProtocol>
</system.webServer>
除了URL Rewrite answer,这里是web.config
的完整XML
<system.webServer>
<rewrite>
<outboundRules>
<rule name="Remove RESPONSE_Server" >
<match serverVariable="RESPONSE_Server" pattern=".+" />
<action type="Rewrite" value="Company name" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
要删除Server:
标题,请转到Global.asax
,查找/创建Application_PreSendRequestHeaders
事件并添加如下行(感谢BK和this blog,这也不会在Cassini / local dev上失败):
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
// Remove the "Server" HTTP Header from response
HttpApplication app = sender as HttpApplication;
if (null != app && null != app.Request && !app.Request.IsLocal &&
null != app.Context && null != app.Context.Response)
{
NameValueCollection headers = app.Context.Response.Headers;
if (null != headers)
{
headers.Remove("Server");
}
}
}
如果您想要一个完整的解决方案来删除Azure / IIS7上的所有相关标头并且也可以使用Cassini,请参阅this link,它显示了在不使用HttpModules或URLScan的情况下禁用这些标头的最佳方法。
如果您只想删除标题,可以使用缩短版本的lukiffer的答案:
using System.Web;
namespace Site
{
public sealed class HideServerHeaderModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders +=
(sender, e) => HttpContext.Current.Response.Headers.Remove("Server");
}
}
}
然后在Web.config
:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="CustomHeaderModule" type="Site.HideServerHeaderModule" />
</modules>
</system.webServer>