【问题标题】:How to remove x-powered-by header in .net core 2.0如何在 .net core 2.0 中删除 x-powered-by 标头
【发布时间】:2021-07-27 20:45:02
【问题描述】:

我尝试使用这个中间件:

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

x-powered-by 仍然存在于响应标头中,上面写着 asp.net

【问题讨论】:

    标签: c# azure security


    【解决方案1】:

    据我所知,这些标头的删除是通过请求过滤模块来实现的,该模块是 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>
    

    将此 web.config 添加到您的网络核心应用程序的根文件夹。

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

    结果如下:

    【讨论】:

    • 是的,这似乎是唯一的方法,因为标头是由 IIS 生成的。这里有一些进一步的信息,包括 .Net Core Hosting 团队的 cmets:github.com/aspnet/Hosting/issues/571
    • 删除Server: Kestrel怎么样?
    【解决方案2】:
    • 除了@Brando Zhang 的回答,从响应头中删除“Server:Kestrel”:

    -.NET Core 1

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

    -NET Core 2

    WebHost.CreateDefaultBuilder(args)
                   .UseKestrel(c => c.AddServerHeader = false)
                   .UseStartup<Startup>()
                   .Build();
    

    【讨论】:

    • 这在项目的 Program.cs 文件中,您应该看到一个现有条目,但没有 .UseKestrel() 调用
    • 这不会删除 x-powered-by 标头!
    • @BerBar 如果您不在 IIS 中运行应用程序,它会这样做......作为控制台应用程序运行它
    • 使用 IIS 管理器、http 标头删除 x-powered-by。只需将它们从列表中删除即可。
    • ".UseKestrel(c => c.AddServerHeader = false)" 仅删除“服务器”标头。它对 X-Powered-By 没有影响
    【解决方案3】:

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

    点击&lt;ServerName&gt; --&gt; HTTP Response Headers --&gt; X-Powered-By 并选择Remove 操作。

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

    【讨论】:

      【解决方案4】:

      作为上述答案的替代选项,您可以使用configuration transformation。这样,web.config 仍将通过 dotnet 发布者 sdk 生成,但可以与特定标签混合,例如删除标头。

      在项目的根目录中创建一个新的web.Release.config 文件,如下所示:

      <?xml version="1.0" encoding="utf-8"?>
      <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <location>
      
          <!-- 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>
            <httpProtocol xdt:Transform="InsertIfMissing">
              <customHeaders>
                <remove name="X-Powered-By" />
              </customHeaders>
            </httpProtocol>
          </system.webServer>
      
        </location>
      </configuration>
      

      请注意,这是一个转换文件,而不是实际的web.config 文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-09
        • 2021-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-29
        • 2018-07-14
        相关资源
        最近更新 更多