在Asp.Net中启用Secure中的HTTPOnly

我们为我们的网站启用了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

点赞