【问题标题】:Remove HTTP server and X-Powered-By headers删除 HTTP 服务器和 X-Powered-By 标头
【发布时间】:2021-05-06 10:13:07
【问题描述】:

我的应用部署在 azure 应用服务上。我的服务器的响应包括以下 HTTP 标头

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

我想从我的回复中永久排除。

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

  1. web.config 文件的变化

    <?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. 更改 Startup.cs 文件

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

这会在 localhost 中产生相同的结果,但在部署时会得到相同的标头。

  1. 编写中间件

    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>();
    

这也会产生相同的结果,在 localhost 中可以,但是当部署到 azure 时会得到相同的标头。 我不是 azure/cloud 专家,但也许 azure 有什么需要改变的?

【问题讨论】:

  • 似乎符合 azure 的直接指导.. azure.microsoft.com/en-us/blog/… - 你确定部署准确吗?
  • 尝试删除 CORS 标头最终可能会导致问题..
  • @NeoXX 有什么更新吗?我的回复对你有帮助吗?

标签: c# azure asp.net-core http-headers azure-web-app-service


【解决方案1】:

根据您的描述,我建议您可以尝试以下方式删除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>

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-28
    • 2018-07-14
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多