加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

在Asp.Net中启用Secure中的HTTPOnly

发布时间:2020-12-16 09:48:53 所属栏目:asp.Net 来源:网络整理
导读:我们为我们的网站启用了HttpOnly. configuration system.web httpCookies httpOnlyCookies="true" requireSSL="true" / /system.web/configuration 当我们在非安全区域访问该站点时,这工作正常.但是在Secure live region(Https)中,这不起作用,我们能够获取会
我们为我们的网站启用了HttpOnly.

<configuration>
    <system.web>
        <httpCookies httpOnlyCookies="true" requireSSL="true" />
    </system.web>
</configuration>

当我们在非安全区域访问该站点时,这工作正常.但是在Secure live region(Https)中,这不起作用,我们能够获取会话密钥.如何减轻它.任何想法都会有所帮助.

我在Asp.Net 2.0中尝试

解决方法

我这里有很多解决方案..请随意选择满足您需求的解决方案

>在Global.asax中,如下覆盖Session_Start方法.

<script runat="server">       
 void Session_Start(object sender,EventArgs e) 
{

    if(Response.Cookies.Count > 0)
    foreach(string s in Response.Cookies.AllKeys)
     if(s == System.Web.Security.FormsAuthentication.FormsCookieName ||
            s.ToLower().Equals("asp.net_sessionid") )
    Response.Cookies[s].HttpOnly = false;

}
???

参考:http://nerd.steveferson.com/2007/09/14/act-sessionid-and-login-problems-with-asp-net-20/#.URiXUqWzd9c

>请注意,在ASP.NET 1.1中,System.Net.Cookie类不支持
HttpOnly属性.因此,要添加一个HttpOnly属性
您可以将以下代码添加到您的应用程序中

Global.asax中的Application_EndRequest事件处理程序:

protected void Application_EndRequest(Object sender,EventArgs e)
{
  string authCookie = FormsAuthentication.FormsCookieName;

  foreach (string sCookie in Response.Cookies)
  {
        if (sCookie.Equals(authCookie))
        {
              Response.Cookies[sCookie].Path += ";HttpOnly";
        }
  }
}

参考:http://blogs.msdn.com/b/dansellers/archive/2006/03/13/550947.aspx

>将此添加到您的Global.asax

void Application_EndRequest(Object sender,EventArgs e)
{
   if (Response.Cookies.Count > 0)
   {
       foreach (string s in Response.Cookies.AllKeys)
       {
           if (s == "ASP.NET_SessionId")
           {
               Response.Cookies["ASP.NET_SessionId"].HttpOnly = false;
           }
       }
   }

}

参考:来自这个论坛http://forums.asp.net/p/955272/1177574.aspx#1177574
您也可以尝试使用Scott Hanselman发布的Post HttpOnly Cookies on ASP.NET 1.1,但是它的ASP.NET 1.1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读