有没有办法从 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");
}
在 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>
Scott Mitchell 在博客文章中提供了删除不必要标题的解决方案。
正如在其他答案中已经说过的那样,对于Server
标头,有http 模块解决方案,或者IIS 10+ 的 web.config 解决方案,或者您可以使用URLRewrite 来代替它。
对于此Server
标头,最新(IIS 10 +)设置的最实用解决方案是removeServerHeader
在 web.config 中使用:
<system.webServer>
...
<security>
<requestFiltering removeServerHeader="true" />
</security>
...
</system.webServer>
对于X-AspNet-Version
and X-AspNetMvc-Version
,Scott Mitchell 提供了一种比在每个响应中删除它们更好的方法:根本不生成它们。
用于enableVersionHeader
禁用X-AspNet-Version
, 在 web.config
<system.web>
...
<httpRuntime enableVersionHeader="false" />
...
</system.web>
在 .Net Application_Start 事件中用于MvcHandler.DisableMvcResponseHeader
禁用X-AspNetMvc-Version
MvcHandler.DisableMvcResponseHeader = true;
最后,在 IIS 配置中删除X-Powered-By
web.config 中的自定义标头。
<system.webServer>
...
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
...
</system.webServer>
请注意,如果您有 ARR(应用程序请求路由),它也会添加自己的X-Powered-By
,不会被自定义标头设置删除。这个必须通过 IIS 管理器、IIS 根目录上的编辑器配置(不在站点上)删除:转到system.webServer/proxy
节点并设置arrResponseHeader
为false
. 在 之后IISReset
,它被考虑在内。
(我在这里找到了这个,除了这篇文章是关于旧的 IIS 6.0 的配置方式。)
不要忘记应用程序代码的解决方案默认情况下不适用于静态内容上生成的标头(您可以激活runAllManagedModulesForAllRequests
for 更改它,但它会导致所有请求运行 .Net 管道)。这不是问题,X-AspNetMvc-Version
因为它没有添加到静态内容上(至少如果静态请求未在 .Net 管道中运行)。
旁注:当目的是掩盖使用过的技术时,您还应该更改标准 .Net cookie 名称(.ASPXAUTH
如果激活了表单身份验证(在 web.configname
中的标签上使用属性forms
),ASP.NET_SessionId
(<sessionState cookieName="yourName" />
在 web.config 中的system.web
标签下使用),__RequestVerificationToken
(更改它通过带有 的代码AntiForgeryConfig.CookieName
,但不幸的是不适用于该系统在 html 中生成的隐藏输入))。
启用 IIS (UrlRewrite) 的 URL 重写模块 2.0 版后,在配置部分<configuration>
➡ <system.webServer>
➡<rewrite>
添加出站规则:
<outboundRules>
<rule name="Remove RESPONSE_Server" >
<match serverVariable="RESPONSE_Server" pattern=".+" />
<action type="Rewrite" value="" />
</rule>
</outboundRules>
实际上,上面显示的编码模块和 Global.asax 示例仅适用于有效请求。
例如,在您的 URL 末尾添加 <,您将获得一个“错误请求”页面,该页面仍然公开服务器标头。许多开发人员忽略了这一点。
显示的注册表设置也不起作用。URLScan 是删除“服务器”标头的唯一方法(至少在 IIS 7.5 中)。
此web.config
设置用于从 ASP.NET 响应中删除所有不必要的标头(至少从 IIS 10 开始):
<system.web>
<!-- Removes version headers from response -->
<httpRuntime enableVersionHeader="false" />
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<!--Removes X-Powered-By header from response -->
<clear />
</customHeaders>
</httpProtocol>
<security>
<!--Removes Server header from response-->
<requestFiltering removeServerHeader ="true" />
</security>
</system.webServer>
请注意,与所有其他方法一样,这会隐藏“应用程序”的所有标题。例如,如果您到达某个默认页面或由 IIS 本身或应用程序外部的 ASP.NET 生成的错误页面,这些规则将不适用。因此,理想情况下,它们应该位于 IIS 的根级别,并且可能会给 IIS 本身留下一些错误响应。
PS IIS 10 中存在一个错误,即使配置正确,它有时也会显示服务器标头。它现在应该已修复,但必须更新 IIS/Windows。
或者在 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 重写答案,这里是完整的 XMLweb.config
<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和此博客,这在 Cassini / 本地开发人员上也不会失败):
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 一起使用,请参阅此链接,它显示了在不使用 HttpModules 或 URLScan 的情况下禁用这些标头的最佳方法。
如果您只想删除标题,可以使用 lukiffer's answer 的缩短版本:
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>
尝试将HKLM\SYSTEM\CurrentControlSet\Services\HTTP\Parameters\DisableServerHeader
注册表项设置REG_DWORD
为1
.
UrlScan还可以使用AlternateServerName=
under删除服务器标头[options]
。
跟进eddiegroves 的回答,根据 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 服务期间,您的服务器将从 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:网络应用程序中的所有响应都将受此影响(包括css和js);
您可以在 Global.asax.cs 文件中添加以下代码
protected void Application_PreSendRequestHeaders()
{
Response.Headers.Remove("Server");
}
上面提出的解决方案结合了以下更改对我有用。在这里,我发布了我的方案和解决方案。
对我来说,我想删除以下标题:
我将这些添加到我的 global.asax 中:
<%@ Application Language="C#" %>
<script runat="server">
protected void Application_PreSendRequestHeaders()
{
Response.Headers.Remove("Server");
Response.Headers.Remove("X-Powered-By");
Response.Headers.Remove("X-AspNet-Version");
Response.Headers.Remove("X-AspNetMvc-Version");
}
</script>
上面的事件没有被触发,所以我在 web.config 中添加了以下内容,然后它就起作用了。
<modules runAllManagedModulesForAllRequests="true" />
为了删除版本标头,我还在 web.config 中添加了以下内容:
<httpRuntime enableVersionHeader="false" />
web.config 中的更改:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<system.web>
<httpRuntime enableVersionHeader="false" />
</system.web>
</configuration>
希望能帮助到你!
我对此进行了研究,并且 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"
我在这里和其他几个类似的堆栈溢出线程上尝试了所有的东西。
我挂了一会儿,因为我在进行配置更改后忘记清除浏览器缓存。如果您不这样做并且该文件在您的本地缓存中,它将使用原始标题(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
这是一个本机代码模块。它能够删除Server 标头,而不仅仅是空白值。默认情况下,它会删除:
IIS 7.5 和可能更新的版本将标题文本存储在iiscore.dll
使用十六进制编辑器,找到字符串和后面的“服务器”一词53 65 72 76 65 72
,并将它们替换为空字节。在 IIS 7.5 中,它看起来像这样:
4D 69 63 72 6F 73 6F 66 74 2D 49 49 53 2F 37 2E 35 00 00 00 53 65 72 76 65 72
与其他一些方法不同,这不会导致性能损失。标头也会从所有请求中删除,甚至是内部错误。