C# 移除Response Header,403调整返回为404Make IIS return a 404 status code instead of 403
Server Information Revealed
For the benefit of those who land here through a google/bing search:: Here's the summary of steps:
Step 1: Create a class that derives from IHttpModule (and IDisposable to clean up when we're done):
public class MyCustomModule : IHttpModule, IDisposable
{
private HttpApplication _httpApplication;
private static readonly List<string> HeadersToCloak = new List<string>
{
"Server",
"X-AspNet-Version",
"X-AspNetMvc-Version",
"X-Powered-By"
};
}
Step 2: Get a reference to the intrinsic context in the IHttpModule.Init method, and assign an event handler to the PreSendRequestHeaders event:
public void Init(HttpApplication context)
{
_httpApplication = context;
context.PreSendRequestHeaders += OnPreSendRequestHeaders;
}
Step 3: Now the headers can be removed like so:
private void OnPreSendRequestHeaders(object sender, EventArgs e)
{
if (null == _httpApplication)
{
return;
}
if (_httpApplication.Context != null)
{
var response = _httpApplication.Response;
HeadersToCloak.ForEach(header => response.Headers.Remove(header));
}
}
Step 4: Now register this module in your root web.config under the system.webserver (if running IIS 7.0 integrated mode more details here):
<configuration>
<system.webServer>
<modules>
<add name="MyCustomModule" type="<namespace>.MyCustomModule "/>
</modules>
</system.webServer>
</configuration>
Hidden Directories Detected On Server
Another way is to create a handler in your web.config file that will return the 404 status code.
namespace MyNameSpace
{
public class NoAccessHandler: IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.StatusCode = 404;
}
#endregion
}
}
in your web.config:
<httpHandlers>
<add verb="*" path="docs/*" validate="false" type="MyNameSpace.NoAccessHandler"/>
</httpHandlers>
<system.webServer>
<handlers>
<add name="NoAccess" verb="*" path="docs/*" preCondition="integratedMode" type="MyNameSpace.NoAccessHandler"/>
</handlers>
</system.webServer>
C# 移除Response Header,403调整返回为404Make IIS return a 404 status code instead of 403的更多相关文章
- RobotFramework下的http接口自动化Get Response header 关键字的使用
Get Response header 关键字用来获取http请求返回的http响应头部数据. 常见的Response Header: Header 解释 示例 Accept-Ranges 表明服务器 ...
- ASP.NET MVC中移除冗余Response Header
本文主要介绍如何优化ASP.NET MVC使用IIS时Response Header中的不必要的信息 默认的,创建一个ASP.NET MVC项目,会在Response Header中包含一些敏感的信息 ...
- 【应用服务 App Service】如何移除App Service Response Header中包含的服务器敏感信息
问题描述 有些情况下,当应用部署到App Service上后,在有些Response Header中,可以看见关于服务器的一些信息,这样会导致隐藏的安全问题,所以可以在web.config中移除某些关 ...
- 【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
问题描述 在Azure App Service上部署了站点,想要在网站的响应头中加一个字段(Cache-Control),并设置为固定值(Cache-Control:no-store) 效果类似于本地 ...
- Tomcat 中响应头信息(Http Response Header) Content-Length 和 Transfer-Encoding
户端(PC浏览器或者手机浏览器)在接受到Tomcat的响应的时候,头信息通常都会带上Content-Length ,一般情况下客户端会在接受完Content-Length长度的数据之后才会开始解析.而 ...
- 转:PHP--获取响应头(Response Header)方法
转:http://blog.sina.com.cn/s/blog_5f54f0be0102uvxu.html PHP--获取响应头(Response Header)方法 方法一: ========== ...
- upstream timed out (110: Connection timed out) while reading response header from upstream, client:
遇到的问题 之前没配置下面这段,访问时候偶尔会出现 504 gateway timeout,由于偶尔出现,所以不太好排查 proxy_connect_timeout 300s;proxy_read_t ...
- recv() failed (104: Connection reset by peer) while reading response header from upstream
2017年12月1日10:18:34 情景描述: 浏览器执行了一会儿, 报500错误 运行环境: nginx + php-fpm nginx日志: recv() failed (104: Conn ...
- HTTP 响应头信息(Http Response Header) Content-Length 和 Transfer-Encoding
Tomcat 中响应头信息(Http Response Header) Content-Length 和 Transfer-Encoding 客户端(PC浏览器或者手机浏览器)在接受到Tomcat的响 ...
随机推荐
- Ubuntu下使用cmake生成可执行文件
1.单个CPP文件的cmake 首先编写一个简单的程序(main.cpp): #include <iostream> using namespace std; int main() { c ...
- incomplete type is not allowed ofstream
错误: incomplete type is not allowed 解决方案: #include<fstream>
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习8
#include <iostream>using namespace std;const int Size=20;struct Pizza{ char company[Size]; dou ...
- Centos 6 安装 Mysql 5.6
参考:http://www.runoob.com/mysql/mysql-database-import.html 数据库 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个 ...
- oracle NVL,NVL2,NULLIF,COALESCE
Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...
- linux之软连接,硬连接篇
作业四: 1) 建立/etc/passwd的软连接文件,放在/tmp目录下 [root@localhost 桌面]# ln -s /etc/passwd/a.txt /tmp/aa.txt 2) 建立 ...
- fixed和sticky
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>f ...
- Servlet(8)—GenericServlet和改进Servlet实例
概念genericservlet是个抽象的父类,必须给出子类才能实例化它.它给出了设计servlet的一些骨架,定义了servlet生命周期,还有一些得到名字.配置.初始化参数的方法,其设计的是和应用 ...
- WPF之几何图形Geometry
在WPF的DrawingContext对象中,提供了基本的绘制椭圆和矩形的API:DrawEllipse和DrawRectangle.但是,这些是远远不够用的,我们在日常应用中,更多的是使用DrawG ...
- PowerBI发布到网页
如果网页当中需要嵌入PowerBI的报表,可以在PowerBI当中生成链接,然后网页或者博客当中插入这一段html代码. 以下是PowerBI生产网页链接的示例,并且在博客的最后也插入了PowerBI ...