Response Header中不暴露Server(IIS)版本、ASP.NET及相关版本等信息

ASP MVC开发的Web默认情况下会在请求的回应中暴露Server、X-AspNet-Version、X-AspNetMvc-Version、X-Powered-By等相关服务端信息,Session ID对应的Cookie也会以“ASP.NET_SessionId”默认命名,公开这些敏感信息会存在一定的安全风险。

X-SourceFiles标头用于被IIS / IIS Express中某些调试模块理解,它包含到磁盘上源文件的base64编码路径,并用于将页面生成的输出链接回该源文件,只在本机请求下生成,应用程序部署到实际服务器时,并不会出现,无需担心!

https://www.cnblogs.com/xixifusigao/p/3953279.html
https://www.orcode.com/question/1214535_kf9ca5.html

(以下转载自:https://www.cnblogs.com/namexiaoqi/p/10981224.html)


一、隐藏:X-AspNetMvc-Version

在Global.asax文件的Application_Start方法中添加:

MvcHandler.DisableMvcResponseHeader = true;

二、移除 Header 中的 Server

在Global.asax文件中添加: 

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
    var app = sender as HttpApplication;
    if (app == null || app.Context == null)
    {
        return;
    }

    // 移除 Server
    app.Context.Response.Headers.Remove("Server");
}

三、移除 X-Powered-By

在Web.config文件中添加:

<system.webServer>
    <!-- 其它内容 -->
    <httpProtocol>
        <customHeaders>
            <!-- 移除 X-Powered-By -->
            <clear />
            <!-- 还可以添加自己的 X-Powered-By 做为标识 -->
            <add name="X-Powered-By" value="bbb.com" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

四、移除X-AspNet-Version

在Web.config文件中<httpRuntime enableVersionHeader="false" />

<httpRuntime enableVersionHeader="false" />

五、更改SessionID默认的“ASP.NET_SessionId” CookieName

在Web.config文件中设置 <sessionState cookieName="要伪装显示的名字" />

(注意,设置完后浏览器要清理下缓存的cookie, 不然设置后依然还是会附带缓存的“ASP.NET_SessionId” Cookie)

<system.web>
    <sessionState cookieName="J_SessionId" />
</system.web>
  • 未清理缓存的情况下,Cookie中依然附带前面缓存的“ASP.NET_SessionId”,但这只是个缓存问题:

  • 清除浏览器缓存的“ASP.NET_SessionId”Cookie,之后不再附带:

  • 清除浏览器Cookie缓存后,请求将不再附带“ASP.NET_SessionId”显示的Cookie, 而是成功以伪装的J_SessionId命名:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值