移除HTTP服务器和X-Powered-By头信息。

3

我的应用程序部署在Azure应用服务上。服务器响应包含以下HTTP头:

Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET

我是一名中文翻译助手,以下是关于IT技术的翻译内容。请注意保留HTML标签。

我希望永久地从我的回复中排除下列内容。

问题如下。我尝试了三件事情:

  1. Changes in web.config file

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
     <system.webServer>
     <httpProtocol>
       <customHeaders>
         <remove name="X-Powered-By" />
         <remove name="Server" />
         <remove name="Access-Control-Allow-Origin" />
       </customHeaders>
     </httpProtocol>
     <security>
       <requestFiltering removeServerHeader="true" />
     </security>
    </system.webServer>
    </configuration>
    

当我在本地主机上运行我的应用程序并发出请求时,我没有得到上述标头,但是当我将其部署在Azure上时,我再次获取了这些标头。

  1. Change Startup.cs file

           app.Use(async (context, next) =>
             {
                 context.Response.Headers.Remove("Server");
                 context.Response.Headers.Remove("X-Powered-By");
                 await next();
             });
    

这会在本地环境下产生相同的结果,但是在部署时会得到相同的头信息。

  1. Write middleware

    public async Task InvokeAsync(HttpContext context)
    {
        context.Response.Headers.Remove("Server");
        context.Response.Headers.Remove("X-Powered-By");
    
        await _next(context);
    }
    
        app.UseMiddleware<HttpMiddleware>();
        app.UseAuthentication();
        app.UseMiddleware<RequestLoggingMiddleware>();
    
这也会产生相同的结果,在本地主机上没问题,但在部署到 Azure 后得到相同的头信息。 我不是 Azure/云服务专家,但可能需要在 Azure 上进行一些更改?

看起来符合 Azure 直接的指导。https://azure.microsoft.com/zh-cn/blog/removing-standard-server-headers-on-windows-azure-web-sites/ - 你确定它已经正确部署了吗? - undefined
试图移除CORS头可能会导致最终出现问题。 - undefined
@NeoXX 有任何更新吗?我的回复对你有帮助吗? - undefined
1个回答

3
根据您的描述,我建议您尝试以下方法来移除X-Powered-By: ASP.NET
如果您将应用程序托管在Linux上,您可以尝试在Program.CS中修改UseKestrel设置。
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                webBuilder.UseKestrel(option => option.AddServerHeader = false);
                
            });

结果:

在这里输入图片描述

如果您将应用程序托管在Windows上,则应修改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" />
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>


</configuration>

结果:

这里输入图片描述


翻译完毕,如有需要请再告诉我。

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