![](/img/trans.png)
[英]API hosted on Azure web app returns JSON Array with out the Name .Net core 3.1
[英]How to remove server info header from web api core 3.1 response which is hosted as azure web app?
我有一個 azure web api 在 Z2D50972FECD376129545507F106 內核中開發。 請求和響應工作正常。 我正在嘗試從 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.
對於 Kestrel:嘗試在 Program.cs 中設置 Kestrel 選項,如下所示。 Kestrel 服務器 header 在請求管道中添加得太晚了。 因此,無法通過 web.config 或中間件將其刪除。 注意:下面我使用了 UseKestrel 而不是 ConfigureKestrel。
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseKestrel(options => options.AddServerHeader = false)
});
對於 IIS:您需要在 web.config 中設置如下:
<configuration>
<system.webServer>
<security>
<requestFiltering removeServerHeader="true" />
</security>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.