到底如何在 ASP.NET 中配置 httpOnlyCookies?

发布于 2024-07-04 02:23:38 字数 170 浏览 8 评论 0原文

受这篇 CodingHorror 文章“保护您的 Cookie:HttpOnly”的启发,

您如何设置这个性质? 在网络配置的某个地方?

Inspired by this CodingHorror article, "Protecting Your Cookies: HttpOnly"

How do you set this property? Somewhere in the web config?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

锦欢 2024-07-11 02:23:38

借助 Rick 的支持(提到的博客文章中的第二条评论),这里是 关于 httpOnlyCookies 的 MSDN 文章

最重要的是,您只需在 web.config 的 system.web 部分中添加以下部分:

<httpCookies domain="" httpOnlyCookies="true|false" requireSSL="true|false" />

With props to Rick (second comment down in the blog post mentioned), here's the MSDN article on httpOnlyCookies.

Bottom line is that you just add the following section in your system.web section in your web.config:

<httpCookies domain="" httpOnlyCookies="true|false" requireSSL="true|false" />
ˇ宁静的妩媚 2024-07-11 02:23:38

如果您使用的是 ASP.NET 2.0 或更高版本,则可以在 Web.config 文件中将其打开。 在中 部分,添加以下行:

<httpCookies httpOnlyCookies="true"/>

If you're using ASP.NET 2.0 or greater, you can turn it on in the Web.config file. In the <system.web> section, add the following line:

<httpCookies httpOnlyCookies="true"/>
深府石板幽径 2024-07-11 02:23:38

有趣的是,在 ASP.NET 2.0 中放置 似乎并没有禁用 httpOnlyCookies 。 查看这篇关于 ASP .NET 2.0 的 SessionID 和登录问题

看起来 Microsoft 决定不允许您从 web.config 禁用它。 查看forums.asp.net 上的帖子

Interestingly putting <httpCookies httpOnlyCookies="false"/> doesn't seem to disable httpOnlyCookies in ASP.NET 2.0. Check this article about SessionID and Login Problems With ASP .NET 2.0.

Looks like Microsoft took the decision to not allow you to disable it from the web.config. Check this post on forums.asp.net

追星践月 2024-07-11 02:23:38

如果您想在代码中执行此操作,请使用 系统。 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,哪些不是。

If you want to do it in code, use the System.Web.HttpCookie.HttpOnly property.

This is directly from the MSDN docs:

// 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);

Doing it in code allows you to selectively choose which cookies are HttpOnly and which are not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文