如何在Asp.Net Core 2.2.1 Web应用程序中移除服务器标头?

4
我正在使用Asp.Net Core 2.2.1。我试图从响应中删除server头信息。我尝试在ConfigureKestrel()中添加options.AddServerHeader = false;,但仍然不成功。请指导我错在哪里。
这是我的代码:

Program.cs

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            return WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .ConfigureKestrel((context,options) => {
                    // Set properties and call methods on options
                    options.AddServerHeader = false;
                });
        }
    }

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>
    <security>
      <requestFiltering removeServerHeader="true" />
    </security>
    <handlers>
      <remove name="aspNetCore" />
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_HTTPS_PORT" value="44342" />
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

响应图像

enter image description here

谢谢,

阿卜杜勒


我认为这个问题需要更改你的IIS配置,而不是你的代码。 - TZHX
请查看另一个问题的此答案:https://dev59.com/vnM_5IYBdhLWcg3w43lt#53222946 - TZHX
@TZHX 上面提到的答案不起作用。我仍然可以在响应中看到服务器头。还有其他帮助吗? - fingers10
2个回答

11

如果您的应用程序在Kestrel上运行,使用options.AddServerHeader = false;调用ConfigureKestrel仅会删除服务器标头。当您将应用程序托管在IIS/IISExpress上时,需要添加以下设置的web.config

<configuration> 
  <system.webServer>
    <security>
      <requestFiltering removeServerHeader="true" />
    </security>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

这行代码 <requestFiltering removeServerHeader="true" /> 可以解决问题。此外,如果你想移除一些自定义的头信息,如 X-Powered-By,你可以在 httpProtocol 下添加 customHeaders 部分。

请确保已启用请求筛选功能。

enter image description here

希望对你有所帮助。


通过IIS Express运行怎么样? - fingers10
你的项目根目录下有web.config文件吗?或者你修改了applicationhost.config文件吗?在发布之前我已经测试过,它可以正常工作,所以你那里一定有些不同。你能否将示例代码上传到GitHub或其他地方? - Shahzad Hassan
1
@Abdul,问题出在你的web.config文件中。它没有<security><requestFiltering removeServerHeader="true" /></security>。请查看屏幕截图WithServerHeaderNoServerHeader。同时,更新了Web.config。一旦添加了安全部分,你将不会看到server头。希望这能帮到你。 - Shahzad Hassan
你是如何在IISExpress中启动应用程序的? - Shahzad Hassan
1
如果你没有使用那些功能,比如基本身份验证等,我认为不需要检查所有内容,只需要检查你需要的部分即可。另外,尝试将其发布到IIS上,并查看发布的代码是否在完整的IIS下工作,因为最终你必须在那里托管 :) - Shahzad Hassan
显示剩余16条评论

2
我们可以使用URLRewrite来实现这一点。请注意,这不会完全删除标题,但会删除其值。

enter image description here

以下是步骤:
第一步。安装URLRewrite。要安装URLRewrite,请转到以下链接。

http://www.iis.net/downloads/microsoft/url-rewrite

步骤2:打开您想要删除服务器标头的站点,并单击URLRewrite部分。

enter image description here

步骤3:单击右侧动作窗格中的“查看服务器变量”。 enter image description here 步骤4:单击“添加”按钮,然后在提供的文本框中输入“RESPONSE_SERVER”。 6562.image_21870933.png 步骤5:现在,我们需要创建一个出站规则。要了解如何创建出站规则,请查看以下链接。 http://www.iis.net/learn/extensions/url-rewrite-module/creating-outbound-rules-for-url-rewrite-modul...
步骤6:创建以下出站规则。 5756.image_036485DD.png 请注意,这是一个特定于网站的规则。如果您想为所有应用程序创建规则,请在服务器级别创建规则。此外,某些应用程序(尤其是第三方应用程序)可能需要Server头,因此您可能需要删除这个规则。

我需要按照这些步骤在开发模式下测试吗? - fingers10
您需要在所需的网站中实施IIS中的这些步骤。项目的模式无关紧要(开发/系统集成测试/生产)。 - Mallikarjuna Golla
通过IIS Express运行怎么样? - fingers10
1
我不确定在开发中为什么你想要隐藏。由于安全限制,此设置将在生产或其他环境中被移除,这样黑客就不会知道我们正在使用哪个Web服务器。 - Mallikarjuna Golla

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