【发布时间】:2020-06-26 16:03:25
【问题描述】:
我有一个在 .net core 3.1 中开发的 azure web api。请求和响应工作正常。我正在尝试从 api 响应中删除服务器标头,但到目前为止没有成功。
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.
【问题讨论】: