简体   繁体   English

为什么这个cookie有时会为空?

[英]Why is this cookie sometimes null?

I have a very strange problem that has been giving me trouble for weeks now. 我有一个非常奇怪的问题,几个星期以来一直给我带来麻烦。 I have an ASP.NET page that gets a value from a cookie like so 我有一个ASP.NET页面,可以从cookie中获取值

string SessionKey = Request.Cookies["SessionKey"].Value;

The cookie itself is set from within a silverlight application running on the same server, using a little helper method I found here on SO. cookie本身是在运行在同一服务器上的silverlight应用程序中设置的,使用我在SO上找到的一个小帮助方法。

public static void SetCookie(string key, string value)
{
   DateTime expiration = DateTime.UtcNow + TimeSpan.FromDays(5);
   string cookie = String.Format("{0}={1};expires={2}", key, value, expiration.ToString("R"));
   HtmlPage.Document.SetProperty("cookie", cookie);
}

So here's the issue. 所以这就是问题所在。 I have three computers. 我有三台电脑。 All three are in separate locations (house, office, different office). 这三个都在不同的位置(房子,办公室,不同的办公室)。 One is a development machine with this whole web application hosted on it. 一个是在其上托管整个Web应用程序的开发机器。 The second is a web server with a copy of the web application running in IIS. 第二个是Web服务器,其中包含在IIS中运行的Web应用程序的副本。 The third is just a plain computer in an office. 第三个是办公室里的普通计算机。

On the dev machine, testing against its own localhost dev environment, this works fine and the cookie is what I expect. 在开发机器上,针对它自己的localhost开发环境进行测试,这很好,而cookie就是我所期望的。

On the dev machine, talking to the server, it also works fine. 在开发机器上,与服务器通信,它也可以正常工作。 The cookie is what it should be, etc etc. 饼干应该是什么,等等。

The third, plain computer, when I connect to the server, I have a null cookie. 第三台普通计算机,当我连接到服务器时,我有一个空cookie。 Doesn't work! 不行!

BUT on the third, plain computer, when I connect to the dev machine (I made it public facing to test this), it works fine and the cookie is populated. 但是在第三台普通计算机上,当我连接到开发机器时(我公开面对它进行测试),它工作正常并且填充了cookie。

All three of these machines are on completely separate networks. 所有这三台机器都在完全独立的网络上。

This issue is not restricted to the third machine, there are lots of machines that, when they talk to the server, the cookie isn't working, but does work if they try to view my dev setup. 这个问题不仅限于第三台机器,有许多机器,当它们与服务器通信时,cookie无法正常工作,但如果它们试图查看我的开发设置,它确实有效。 Even stranger, every now and then it DOES work, but very inconsistently. 即使是陌生人,也时不时地工作,但非常不一致。 Any ideas what would be causing this? 会有什么想法导致这种情况?

Things to check 要检查的事情

  • cookies path, just add it as "{0}={1};expires={2};path=/;" cookies路径,只需将其添加为“{0} = {1}; expires = {2}; path = /;”
  • if server name has only alphanumeric characters (no underscore or other special characters); 如果服务器名称只有字母数字字符(没有下划线或其他特殊字符); try to use IP address rather than the domain/server name or vice versa 尝试使用IP地址而不是域/服务器名称,反之亦然
  • if cookies could be set from test aspx-page 如果可以从测试aspx页面设置cookie

You can use Fiddler to see if you can get more insight into this. 您可以使用Fiddler来查看是否可以更深入地了解这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM