How to secure the ASP.NET_SessionId cookie?

后端 未结 6 2064
难免孤独
难免孤独 2020-12-02 05:58

I have set the .ASPXAUTH cookie to be https only but I am not sure how to effectively do the same with the ASP.NET_SessionId.

The entire site uses HTTPS so there is

6条回答
  •  [愿得一人]
    2020-12-02 06:25

    Adding onto @JoelEtherton's solution to fix a newly found security vulnerability. This vulnerability happens if users request HTTP and are redirected to HTTPS, but the sessionid cookie is set as secure on the first request to HTTP. That is now a security vulnerability, according to McAfee Secure.

    This code will only secure cookies if request is using HTTPS. It will expire the sessionid cookie, if not HTTPS.

        // this code will mark the forms authentication cookie and the
        // session cookie as Secure.
        if (Request.IsSecureConnection)
        {
            if (Response.Cookies.Count > 0)
            {
                foreach (string s in Response.Cookies.AllKeys)
                {
                    if (s == FormsAuthentication.FormsCookieName || s.ToLower() == "asp.net_sessionid")
                    {
                        Response.Cookies[s].Secure = true;
                    }
                }
            }
        }
        else
        {
            //if not secure, then don't set session cookie
            Response.Cookies["asp.net_sessionid"].Value = string.Empty;
            Response.Cookies["asp.net_sessionid"].Expires = new DateTime(2018, 01, 01);
        }
    

提交回复
热议问题