如何在.net core 2.0中删除x-powered-by标头

ame*_*els 15 c# security azure

我试图使用这个中间件:

public class SecurityHeadersMiddleware
{
    private readonly RequestDelegate next;

    public SecurityHeadersMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        context.Response.OnStarting(state =>
        {
            var ctx = (HttpContext)state;

            if (!ctx.Response.Headers.ContainsKey("Arr-Disable-Session-Affinity"))
            {
                ctx.Response.Headers.Add("Arr-Disable-Session-Affinity", "True"); // Disables the Azure ARRAffinity cookie
            }

            if (ctx.Response.Headers.ContainsKey("Server"))
            {
                ctx.Response.Headers.Remove("Server"); // For security reasons
            }

            if (ctx.Response.Headers.ContainsKey("x-powered-by") || ctx.Response.Headers.ContainsKey("X-Powered-By"))
            {
                ctx.Response.Headers.Remove("x-powered-by");
                ctx.Response.Headers.Remove("X-Powered-By");
            }

            if (!ctx.Response.Headers.ContainsKey("X-Frame-Options"))
            {
                ctx.Response.Headers.Add("X-Frame-Options", "DENY");
            }

            return Task.FromResult(0);
        }, context);

        await next(context);
    }
}
Run Code Online (Sandbox Code Playgroud)

x-powered-by仍然存在于响应标题中,表示asp.net

Bra*_*ang 30

据我所知,使用请求过滤模块可以帮助删除这些标头,该模块是IIS的一部分.

要删除标题,您需要在您的网站上存储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>
Run Code Online (Sandbox Code Playgroud)

将此web.config添加到net core应用程序的根文件夹中.

然后它将删除x-powered-by标头.

结果如下:

在此输入图像描述

  • 删除“服务器:Kestrel”怎么样? (2认同)

Ahm*_*bry 23

  • 要从响应头中删除"Server:Kestrel",您需要告诉Kestral删除其标头

-.NET Core 1

 var host = new WebHostBuilder()
        .UseKestrel(c => c.AddServerHeader = false)
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();
Run Code Online (Sandbox Code Playgroud)

-NET Core 2

WebHost.CreateDefaultBuilder(args)
               .UseKestrel(c => c.AddServerHeader = false)
               .UseStartup<Startup>()
               .Build();
Run Code Online (Sandbox Code Playgroud)

  • 这不会删除x-by-by标头! (10认同)
  • 这在项目的Program.cs文件中,你应该看到一个现有的条目,但没有.UseKestrel()调用 (4认同)
  • “ .UseKestrel(c =&gt; c.AddServerHeader = false)”仅删除“服务器”标头。它对X-Powered-By没有影响 (3认同)
  • 使用 IIS 管理器、http 标头删除 x-powered-by。只需将它们从列表中删除即可。 (2认同)

Rub*_*nov 9

如果不想在 ASP.NET Core 解决方案中创建 web.config 文件,可以X-Powered-By在 IIS 管理器中删除标头。

单击<ServerName> --> HTTP Response Headers --> X-Powered-By并选择Remove操作。

信息系统

这将删除该服务器上所有网站的标头。这很好,因为您为什么要首先分享该信息?