ASP.NET:请求 cookie 的域为空?

发布于 2024-09-11 00:52:02 字数 96 浏览 6 评论 0原文

当我检查 HttpContext.Current.Request.Cookies 集合时,我的一些 cookie 的域成员为 null。

为什么/什么时候域为空?

When I examine my HttpContext.Current.Request.Cookies collection, some of my cookies have null for their Domain member.

Why/when is a Domain null?

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

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

发布评论

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

评论(3

月光色 2024-09-18 00:52:05

域属性仅用于设置cookie。显然,如果您将 cookie 作为请求的一部分进行读取,则客户端浏览器会认为该域与您的站点适当匹配。

The domain property is only for setting cookies. Obviously, if you are reading the cookie as part of the request, the client browser felt that the domain was appropriately matched to your site.

过度放纵 2024-09-18 00:52:05

默认情况下,Cookie 与当前域关联。

所以如果在现场

www.foo.com

并且您执行以下操作:

HttpCookie appCookie = new HttpCookie("AppCookie");
appCookie.Value = "written " + DateTime.Now.ToString();
appCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(appCookie);

该域名将是

www.foo.com

但是,您可以通过设置域的范围来覆盖此功能:

Response.Cookies["AppCookie"].Domain = "bar.foo.com";

这样 cookie 将仅适用于该特定子域中的请求。

当然,您可以将域设置为 NULL,但我无法想象这会有用的场景。

检查您如何创建 cookie。

参考: http://msdn.microsoft.com/en-us/library/ ms178194.aspx

By default, Cookies are associated with the current domain.

So if on site

www.foo.com

and you do the following:

HttpCookie appCookie = new HttpCookie("AppCookie");
appCookie.Value = "written " + DateTime.Now.ToString();
appCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(appCookie);

The domain will be

www.foo.com

.

However, you can override this functionality by setting the scope of the domain:

Response.Cookies["AppCookie"].Domain = "bar.foo.com";

The cookie will then only be available to requests in that specific subdomain.

So of course, you can set the Domain to NULL, but i cant envision a scenario where this would be useful.

Check how you are creating your cookies.

Reference: http://msdn.microsoft.com/en-us/library/ms178194.aspx

凉薄对峙 2024-09-18 00:52:05

HttpCookie 的当 .Net Framework 构造 HttpCookie 类的实例并根据运行时配置文件 (system.web/httpCookies) 中存储的值初始化其值时,Domain 成员为 null,并且该成员不会被其他值(未指定)覆盖。这个未指定值表示cookie应该由用户代理(通常是浏览器)发送到仅发送到生成cookie的服务器而不是其他服务器。

根据互联网提出的标准RFC6265 HTTP状态管理机制

如果服务器省略了Domain属性,用户代理将
仅将cookie返回到源服务器。

警告:某些现有的用户代理会处理不存在的域
属性就像域属性存在并包含一样
当前主机名。例如,如果 example.com 返回
没有 Domain 属性的 Set-Cookie 标头,这些用户代理将
错误地将 cookie 发送到 www.example.com

RFC 没有为未指定定义任何特定值,因此实现者可以选择他们喜欢的任何值。 Microsoft 选择将 Domain 成员表示为字符串值,因此 null 或 "" (String.Empty) 表示未指定,但隐式配置的默认值为null

如果您收到的 cookie 的 Domain 值为 null,则意味着用户代理处理了该 cookie,确定其来源与其向其发送请求的服务器相匹配,并将 cookie 数据包含在请求的Cookie 标头。同样,如果您要返回该 cookie,您可能希望仅在运行您的应用程序的同一主机的另一个请求中接收该 cookie。

在 ASP.Net 应用程序中,如果 string.IsNullOrEmpty(cookie.Domain) 为 true,您可以假设用户代理在向其确定为原始主机的服务器的请求中包含 cookie,即 <代码>Request.Url.Host。

An HttpCookie's Domain member is null when the .Net Framework constructs an instance of the HttpCookie class, initializes its value from values stored in a runtime configuration file (system.web/httpCookies), and the member is not overwritten with another value (not specified). This unspecified value indicates the cookie should be sent by the user agent (usually a browser) to only the server that originated the cookie and no other.

According to the Internet proposed standard RFC6265 HTTP State Management Mechanism:

If the server omits the Domain attribute, the user agent will
return the cookie only to the origin server.

WARNING: Some existing user agents treat an absent Domain
attribute as if the Domain attribute were present and contained
the current host name. For example, if example.com returns a
Set-Cookie header without a Domain attribute, these user agents will
erroneously send the cookie to www.example.com as well.

Th RFC does not define any specific value for unspecified, so implementors may choose whatever value or values they like. Microsoft chose to represent the Domain member as a string value, so either null or "" (String.Empty) represent unspecified, but the implicitly configured default value is null.

If you are receiving a cookie that has Domain value of null, it means the user agent processed the cookie, decided its origin matched the server it was sending the request to, and included the cookie data in the request's Cookie header. Likewise, if you are returning that cookie, you may expect to receive that cookie in another request only by the same host that is running your application.

In an ASP.Net application, if string.IsNullOrEmpty(cookie.Domain) is true, you may assume the user agent included the cookie in the request to the server it decided was the originating host, i.e. Request.Url.Host.

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