【发布时间】:2010-09-07 04:56:46
【问题描述】:
受这篇 CodingHorror 文章“Protecting Your Cookies: HttpOnly”的启发
你如何设置这个属性?网络配置中的某处?
【问题讨论】:
标签: asp.net cookies xss httponly
受这篇 CodingHorror 文章“Protecting Your Cookies: HttpOnly”的启发
你如何设置这个属性?网络配置中的某处?
【问题讨论】:
标签: asp.net cookies xss httponly
如果您使用的是 ASP.NET 2.0 或更高版本,您可以在 Web.config 文件中将其打开。在
<httpCookies httpOnlyCookies="true"/>
【讨论】:
借助 Rick 的道具(提到的博客文章中的第二条评论),这里是 httpOnlyCookies 上的 MSDN article。
底线是您只需在 web.config 的 system.web 部分中添加以下部分:
<httpCookies domain="" httpOnlyCookies="true|false" requireSSL="true|false" />
【讨论】:
如果您想在代码中执行此操作,请使用 System.Web.HttpCookie.HttpOnly 属性。
这直接来自 MSDN 文档:
// Create a new HttpCookie.
HttpCookie myHttpCookie = new HttpCookie("LastVisit", DateTime.Now.ToString());
// By default, the HttpOnly property is set to false
// unless specified otherwise in configuration.
myHttpCookie.Name = "MyHttpCookie";
Response.AppendCookie(myHttpCookie);
// Show the name of the cookie.
Response.Write(myHttpCookie.Name);
// Create an HttpOnly cookie.
HttpCookie myHttpOnlyCookie = new HttpCookie("LastVisit", DateTime.Now.ToString());
// Setting the HttpOnly value to true, makes
// this cookie accessible only to ASP.NET.
myHttpOnlyCookie.HttpOnly = true;
myHttpOnlyCookie.Name = "MyHttpOnlyCookie";
Response.AppendCookie(myHttpOnlyCookie);
// Show the name of the HttpOnly cookie.
Response.Write(myHttpOnlyCookie.Name);
在代码中执行此操作可让您有选择地选择哪些 cookie 是 HttpOnly 的,哪些不是。
【讨论】:
有趣的是,在 ASP.NET 2.0 中添加 <httpCookies httpOnlyCookies="false"/> 似乎并没有禁用 httpOnlyCookies。查看这篇关于SessionID and Login Problems With ASP .NET 2.0的文章。
看起来 Microsoft 决定不允许您从 web.config 禁用它。检查这个post on forums.asp.net
【讨论】: