我正在尝试从Azure Web App(带有ASP Net核心应用程序)中删除响应服务器标头
经过多次尝试更改web.config并使用中间件删除应用程序代码中的标头之后,Microsoft不会放弃并将响应标头设置为Server:Microsoft-IIS / 10.0:)
[仅当我尝试通过http(不是https)访问服务器时,才会出现此问题。服务器的响应代码为301,这是唯一具有服务器标头的响应。
检查日志,我找不到对http://的任何请求,这也许就是为什么我无法删除标头的原因,因为该请求未在我的应用程序代码中处理。
我正在考虑的解决方案是禁用azure 仅HTTPS,并在我的代码中重定向到https(我已测试并且正在工作-服务器头已删除)
是否有另一种解决方法而不禁用仅HTTPS选项?
这是我尝试过的
Startup.cs
public void Configure(IApplicationBuilder app)
{
app.Use(async (context, next) =>
{
context.Response.Headers.Add("server", string.Empty)
}
app.UseHttpsRedirection();
}
web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<httpRuntime enableVersionHeader="false" />
<!-- Removes ASP.NET version header. -->
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="Server" />
<remove name="X-Powered-By" />
</customHeaders>
<redirectHeaders>
<clear />
</redirectHeaders>
</httpProtocol>
<security>
<requestFiltering removeServerHeader="true" />
<!-- Removes Server header in IIS10 or later and also in Azure Web Apps -->
</security>
<rewrite>
<outboundRules>
<rule name="Change Server Header"> <!-- if you're not removing it completely -->
<match serverVariable="RESPONSE_Server" pattern=".+" />
<action type="Rewrite" value="Unknown" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
UPDATE