简体   繁体   English

How to remove server info header from web api core 3.1 response which is hosted as azure web app?

[英]How to remove server info header from web api core 3.1 response which is hosted as azure web app?

I have an azure web api developed in .net core 3.1.我有一个 azure web api 在 Z2D50972FECD376129545507F106 内核中开发。 Request and response working fine.请求和响应工作正常。 I am trying to remove server header from api response but no successes so far.我正在尝试从 api 响应中删除服务器 header 但到目前为止没有成功。

在此处输入图像描述

 Tried below things,

option 1) Added in programe.cs

 static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>{webBuilder.ConfigureKestrel(serverOptions =>
            {
                serverOptions.AddServerHeader = false;
            }).UseStartup<Startup>();                    
            });

option 2) Added in Configure method of startup.cs 
       app.Use(async (context, next) =>
        {
            context.Response.OnStarting(() =>
                {
                    int responseStatusCode = context.Response.StatusCode;
                    if (responseStatusCode == (int)HttpStatusCode.Created)
                    {
                        IHeaderDictionary headers = context.Response.Headers;
                        StringValues locationHeaderValue = string.Empty;
                        if (headers.TryGetValue("Server", out locationHeaderValue))
                        {
                        context.Response.Headers.Remove("Server");
                        }
                    }
                    return Task.FromResult(0);
                    });

option 3) Added in Configure method of startup.cs 
 app.Use(async (context, next) =>  
            {  
                context.Response.Headers.Remove("Server");
                await next();  
            }

Either of these did not worked in my case. Am i missing anything here?
Please provide your suggestions.

For Kestrel: Try setting Kestrel options like below snippet in Program.cs.对于 Kestrel:尝试在 Program.cs 中设置 Kestrel 选项,如下所示。 The Kestrel Server header gets added too late in the request pipeline. Kestrel 服务器 header 在请求管道中添加得太晚了。 Therefore removing it via the web.config or via middleware is not possible.因此,无法通过 web.config 或中间件将其删除。 Note: Below I used UseKestrel instead of ConfigureKestrel.注意:下面我使用了 UseKestrel 而不是 ConfigureKestrel。

       public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.UseKestrel(options => options.AddServerHeader = false)
                });

For IIS: You need to set in web.config like below:对于 IIS:您需要在 web.config 中设置如下:

<configuration> 
  <system.webServer>
    <security>
      <requestFiltering removeServerHeader="true" />
    </security>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

Hope it helps.希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 托管在 Azure Web 应用程序上的 API 返回没有名称 .Net core 3.1 的 JSON 数组 - API hosted on Azure web app returns JSON Array with out the Name .Net core 3.1 Azure App Servce 的 .Net Core 3.1 Web API 延迟问题 - .Net Core 3.1 Web API latency issue with Azure App Servce 筛选对从Web App Azure托管到Azure的sql Server的访问 - Filter access to sql Server hosted to Azure from Web App Azure 从 Azure 中托管的 Asp.net Core MVC Web App 获取“响应状态代码并不表示成功:502(坏网关)” - Getting "Response status code does not indicate success: 502 (Bad Gateway)" from Asp.net Core MVC Web App hosted in Azure 从 Azure 托管的 .Net Core 3.1 应用程序访问 Kroger API 时禁止 403 - 403 Forbidden when accessing Kroger API from Azure-Hosted .Net Core 3.1 app 当托管在Azure应用程序服务中时,如何控制添加到ASP.NET核心Web应用程序中的HTTP响应标头? - How can I control the HTTP response headers added to my ASP.NET core web application when hosted in Azure app service? 如何为 Azure 上托管的 Web 应用程序和 Windows 的 IIS 服务器上托管的 Web 应用程序使用相同的自定义域? - How to use same custom domain for Web app hosted on Azure and web application hosted on window's IIS server? Chrome中Azure中托管的ASP.NET Core Web应用上的异常HTTP响应 - Unusual HTTP response on ASP.NET Core web app hosted in Azure in Chrome 如何从Azure Web-Apps中删除过多的响应头信息? - How can I remove excessive response header information from Azure Web-Apps? 将 .net core 3.1 Web 应用部署到 Azure Linux 应用服务 - Deploying .net core 3.1 web app to Azure Linux App Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM