asp.net - 如何从应用服务 Web 应用程序上的 http 响应 header 中删除 "Microsoft-HTTPAPI/2.0"

标签 asp.net azure azure-web-app-service

我创建了两个 asp.net + MVC 应用程序,并将一个部署到 Azure 应用服务 Web 应用程序,另一个部署到在 ASE(应用程序服务环境)中创建的应用程序服务 Web 应用程序。

在 URL 中提供特殊字符时,响应 header 由“Microsoft-HTTPAPI/2.0”组成。我已在应用程序中进行了以下更改,但问题仍然存在。

<security>
  <requestFiltering removeServerHeader="true"/>
</security>

protected void Application_PreSendRequestHeaders(Object source, EventArgs e)
{
    HttpContext.Current.Response.Headers.Remove("Server");
}

最佳答案

删除默认 header 。您可以创建一个 http 模块来执行此操作。以下代码供您引用。

public class RemoveDefaultHeaderModule : IHttpModule
{
    public void Dispose()
    {

    }

    public void Init(HttpApplication context)
    {
        context.PreSendRequestHeaders += Context_PreSendRequestHeaders;
    }

    private void Context_PreSendRequestHeaders(object sender, EventArgs e)
    {
        //Remove the header you wanted
        (sender as HttpApplication).Response.Headers.Remove("Server");
        (sender as HttpApplication).Response.Headers.Remove("X-AspNet-Version");
    }
}

您还需要在 web.config 中注册此模块。不要忘记将 runAllManagedModulesForAllRequests 属性设置为 true,这将使该模块适用于静态资源。

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="RemoveDefaultHeaderModule" type="TestServerHeader.RemoveDefaultHeaderModule" />
  </modules>
</system.webServer>

关于asp.net - 如何从应用服务 Web 应用程序上的 http 响应 header 中删除 "Microsoft-HTTPAPI/2.0",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43779890/

相关文章:

c# - 无法加载文件或程序集 'DocumentDB.Spatial.Sql' 或其依赖项之一

Linux 上的 Azure Web 应用程序 : "Error: Container didn' t respond to HTTP pings on port: 808 0"- when using: "start": "pm2 start server.js"

css 正在反向层次结构中应用

c# - 局部 View 中的 ASP.NET MVC 验证并返回到父 View

c# - WebClient的UploadFile大小限制

Azure 服务总线 - 确定事件连接数(主题/队列)

javascript - 如何在我使用ul和li标签的情况下使用jQuery Datatable实现分页

c# - 如何访问 CheckBoxList 中 ListItems 的键

mongodb - 如何将 mongoDB Compass 连接到在 azure VM 中运行的 mongoDB 容器?

.net - 在同一个azure web应用程序上创建虚拟目录