从ASP.NET Core 2.1应用程序中删除“Server”标头

42

在运行在 Server 2016 with IIS 10 上的 ASP.NET Core 2.1 应用程序中,是否可以删除服务器响应标头?

我尝试将以下内容放入 web.config 文件中:

<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="X-Frame-Options" value="sameorigin" />
            <add name="X-XSS-Protection" value="1; mode=block" />
            <add name="X-Content-Type-Options" value="nosniff" />
            <remove name="X-Powered-By" />
            <remove name="Server" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

前四个更改操作都很顺利,但是Server标头没有被移除。我仍然看到"Kestrel"

7个回答

88

这个解决方案适用于IIS 10+版本,并允许在服务器响应中去除x-powered-byserver标头。

在IIS 10中添加了一个新属性:removeServerHeader

我们需要在asp.net core应用程序中创建web.config文件,并使用以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering removeServerHeader="true" />
    </security>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

然后发布应用程序并在IIS上重新启动网站。


5
我也注意到了 - Visual Studio确实会说“不允许使用'removeServerHeader'属性”,并用摇晃的绿色线条强调它,但是它确实可以像建议的那样工作。感谢@user3172616! - Brett Rigby
我能否在我的代码中编写一些内容,以便在发布时自动生成web.config文件? - WarrenG
这对我很有效,我的API托管到IIS 10上并且已经移除了“Server”头。 - Gautam Sharma
有没有办法让早期版本的IIS忽略这个属性?这个removeServerHeader属性会导致Web应用在IIS 8中无法启动 :( - nvirth
1
@HamidSiaban - 感谢您的回复。我已经在我的项目中解决了这个问题。我通过手动添加到我的项目中的web.config文件来修复它。由于我们的应用程序正在运行在IIS中,我可以使用它来删除服务器标头。 - aj go
显示剩余6条评论

65

由于Kestrel服务器头在请求管道中加入的时间太晚,因此无法通过Web.config或中间件来删除它。

您可以通过将KestrelServerOptions上的AddServerHeader属性设置为false来删除Server头,这可以在Program.cs中完成。

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseKestrel(options => options.AddServerHeader = false)
            .UseStartup<Startup>();

9
这并不会移除头信息。Kestrel向IIS提供响应,而IIS会添加这个头信息。如果要移除这个头信息,您需要配置web.config文件。 - Sumit Joshi
你必须做所有的事情来移除这个头部。这个设置可以防止它被设置,但是 Kestrel 并不是唯一想让黑客知道你的服务器正在运行什么技术的地方。同样要覆盖 web config(如下一个答案所述),那么你就大功告成了。 - speciesUnknown
如果您不使用IIS,这将起作用。 - user692942

22
在.NET6中,它变成了:
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseKestrel(option => option.AddServerHeader = false);

9

如果有人正在尝试做同样的事情(移除由Kestrel Web服务器添加的服务器响应标头),但是使用ASP.NET Core 2.2,他们应该使用扩展方法ConfigureKestrel (https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.webhostbuilderkestrelextensions.configurekestrel?view=aspnetcore-2.2#Microsoft_AspNetCore_Hosting_WebHostBuilderKestrelExtensions_ConfigureKestrel_Microsoft_AspNetCore_Hosting_IWebHostBuilder_System_Action_Microsoft_AspNetCore_Server_Kestrel_Core_KestrelServerOptions__),而不是使用UseKestrel扩展方法。


1
嗯,对我来说 ConfigureKestrel(x => x.AddServerHeader = false) 没有效果。头信息仍然存在。 - Sam Alekseev
@user3172616 你是从Visual Studio运行应用程序还是从具有自己服务器的测试环境中运行的? - Enrico Massone
应用程序正在远程生产服务器IIS10上运行(而不是Kestrel)。无论如何,我在这个主题中找到了另一种解决方案并发表了帖子。 - Sam Alekseev
@user3172616 注意,Kestrel的配置仅影响Kestrel本身设置的HTTP响应头。通常情况下,在运行ASP.NET Core应用程序时,您会使用一个反向代理服务器位于应用程序前面。这意味着反向代理有机会添加自己的响应头。例如,如果您使用IIS(即使是IIS Express),您将发现由IIS添加的Server头。 - Enrico Massone
@user3172616 在我上面提到的情况下,您必须配置反向代理服务器放在 asp.net core 应用程序前面,以便剥离其自己的 Server 标头。扩展方法 ConfigureKestrel(x => x.AddServerHeader = false) 只影响 Kestrel 的标头。 - Enrico Massone
我误解了你的帖子回答,很抱歉。实际上,这些标头是在IIS级别上设置的,所以我使用了web.config文件来配置这个行为。 - Sam Alekseev

8
对于Dotnet Core 3.1,UseKestrel已经成为ConfigureWebHostDefaults的一部分,而不是之前版本中的CreateDefaultBuilder
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>()
                      .UseKestrel(options => options.AddServerHeader = false);
        });

16
这个方法可行。值得一提的是,如果你使用这种修复方式,就无法在Visual Studio中使用IISExpress运行应用程序。你会收到以下错误提示: System.InvalidOperationException: 'Application is running inside IIS process but is not configured to use IIS server.' - Tom Chantler
6
我也遇到了同样的问题。在 IIS 服务器上,使用 UseKestrel 无法运行您的应用程序。 - Bernard Nongpoh
有没有办法可以删除Kestrel添加的“Content-Length”头? - user804401

5

这些说明仅适用于IIS 10.0。

  1. Open the web.config file located in the root directory for the website.

  2. Configure requestFiltering in the web.config system.webServer node:

    
    <security>
        <requestFiltering removeServerHeader ="true" />
    </security>
    
    
  3. Save the file and restart your IIS app.


2
Orion 网站是什么? - user369117
抱歉,不是“Orion”,只是“网站”。 - Vladyslav Fomin

0

@SamAlekseev的回答非常好,可以删除ServerX-Powered-By头信息。唯一缺少的是也要删除X-AspNet-Version。这对于Azure App ServicesIIS都适用。

web.config完整代码:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!-- To customize the asp.net core module uncomment and edit the following section. 
  For more info see https://go.microsoft.com/fwlink/?linkid=838655 -->
  <!--
  <system.webServer>
    <handlers>
      <remove name="aspNetCore"/>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
  </system.webServer>
  -->
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
    <security>
      <requestFiltering removeServerHeader="true" />
    </security>
  </system.webServer>
  <system.web>
    <httpRuntime enableVersionHeader="false"/>
  </system.web>
</configuration>

源代码:

https://azure.microsoft.com/en-us/blog/removing-standard-server-headers-on-windows-azure-web-sites/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接