【问题标题】:asp.net core remove X-Powered-By cannot be done in middlewareasp.net core remove X-Powered-By 不能在中间件中完成
【发布时间】:2016-08-26 21:31:07
【问题描述】:

为什么我不能将 X-Powered-By 作为我正在执行的中间件的一部分删除?如果我把它放在 web.config 中,我可以删除它,但如果我把它放在中间件中,我就不能删除它。我正在删除中间件“服务器”中的另一个标头:“Kestrel”,它可以工作并告诉我我的中间件正在执行。

我正在使用 Visual Studio 2015、ASP.Net Core Web 应用程序 (.NET Framework)、1.0.0-rc2-final

我的中间件

public class ManageHttpHeadersMiddleware
{
    private RequestDelegate _next;

    public ManageHttpHeadersMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        context.Response.OnStarting(() =>
        {
            context.Response.Headers.Remove("Server");
            context.Response.Headers.Remove("X-Powered-By");

            return Task.CompletedTask;
        });

        await _next(context);
    }
}

我的 Startup.Configure 方法如下所示

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddSerilog(new LoggerConfiguration()
            .ReadFrom.ConfigurationSection(Configuration.GetSection("Serilog"))
            .CreateLogger())
            .AddDebug();

        app.UseMiddleware<ManageHttpHeadersMiddleware>();

        app.UseJwtBearerAuthentication();

        app.UseMvc();

        app.UseSwaggerGen();
        app.UseSwaggerUi();
    }

所以我的问题是:

  1. 是不是因为我执行中间件的顺序 启动配置?
  2. 是不是因为我正在执行的事件 中间件?我曾尝试使用 OnCompleted,但显然为时已晚,并且没有删除 "Server" : "Kestrel"
  3. 是不是因为它 由 Kestrel 或 IIS 在 Azure 中添加,唯一的删除方法是通过 web.config ?

我知道您可能会争辩说我有一个解决方法以及我的问题是什么,但是在相同的代码位置实现相同的要求、帮助可维护性等会很好。

【问题讨论】:

标签: c# asp.net-core header middleware


【解决方案1】:

为您查询有关 X-Powered-By 的问题。你是对的,你必须这样做 web.config 文件。(当你使用 IIS 时)并且当任何这样的标头由服务器进行时,你必须手动或以不同方式维护,因为这不是 ASP.net 核心的一部分。

如果您了解我们托管时的 ASP.net 核心请求管道就像

  1. 浏览器 - IIS - Kestrel (Windows)
  2. 浏览 - NGinx - Kestrel (Linux)

当任何请求处理 Kestrel 时,它会移交给 IIS 或 Nginx,然后 IIS 或 NGInx 可能会添加标头。 X-Powered-By 就是这样的标题。所以我们可以删除它 web.config 或直接在 IIS 设置中。

注意:到目前为止,我觉得我们不会在旧的 ASP.net / ASP.net MVC 中得到任何这样的东西,我们在其中创建 HTTPModule 并且我们能够删除所有类型标头。这是可能的,因为它与 IIS 紧密集成。

注意:我已经发表了评论,但为了更清楚地说明我无法使用中间件删除服务器标头。即使我尝试了你的代码。 (我尝试过使用 IIS)。

要删除服务器标头,我必须做以下事情。

new WebHostBuilder()
    .UseKestrel(c => c.AddServerHeader = false)

【讨论】:

  • 这不会删除 x-powered-by 标头!
  • 通过 dotnet core 是不可能的,但在 iis 级别使用模块是可能的。这就是我上面解释的内容
【解决方案2】:

我们可以删除X-Powered-By 和其他带有web.config 的标头,因为它再次添加到asp.net core 中

<httpProtocol>
  <customHeaders>
    <remove name="X-Powered-By" />
  </customHeaders>
</httpProtocol>

【讨论】:

【解决方案3】:

这是 dotnet core 3.1 应用程序根目录中的完整 web.config,它删除了 X-Powered-ByServer 标头。当您从 Project > Add > New item > Web Config file 添加文件时,其他内容是默认的

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <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" />
    <httpProtocol>
      <!-- Remove X-Powered-By header -->
      <customHeaders>
        <remove name="X-Powered-By" />
        <remove name="Server" />
      </customHeaders>
    </httpProtocol>
    <security>
        <!-- Remove Server header-->
        <requestFiltering removeServerHeader ="true" />
    </security>
  </system.webServer>
</configuration>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    相关资源
    最近更新 更多