Asp.net HttpCookie 读/写问题
我一直在寻找解决这个问题的方法,但我找不到。顺便说一句,我不明白我的问题的原因。
问题是:
我的 Web 应用程序有一个登录页面,并从 cookie 获取记录的用户 ID。它以前可以工作,但 5-6 天前,由于某些更改,它无法在 IE 上工作。现在它不适用于任何浏览器。
我可以在 Chrome 中看到 cookie。当使用 Internet Explorer 开发工具查看时,有时会写入 cookie,但 IE 仍然无法读取
我的 Web 应用程序位于 Windows Server 2008 R2 顺便说一句,
设置我的 web.config:
<httpCookies domain=".domainname.com" httpOnlyCookies="false" requireSSL="false" />
这是我的 SetCookie 代码
<!-- language: c# -->
string uId = "userID";
DateTime expireDate = DateTime.Now.AddDays(3);
HttpContext.Current.Response.Cookies["cookieName"]["uID"] = uId;
HttpCookie aCookie = new HttpCookie("cookieName");
aCookie.Values["uID"] = uId;
aCookie.Path = "/";
aCookie.Expires = expireDate;
aCookie.HttpOnly = false;
aCookie.Domain = "domainname.com";
aCookie.Name = "cookieName";
HttpContext.Current.Response.Cookies.Add(aCookie);
和此 GetCookie 代码
<!-- language: c# -->
if (HttpContext.Current.Request.Cookies["cookieName"] != null)
{
System.Collections.Specialized.NameValueCollection UserInfoCookieCollection;
UserInfoCookieCollection = HttpContext.Current.Request.Cookies["cookieName"].Values;
userID = HttpContext.Current.Server.HtmlEncode(UserInfoCookieCollection["uID"]);
}
场景是:
<块引用>尝试登录
触发SetCookie方法
SetCookie方法结束有两个cookie“cookieName”和 “ASP.NET 会话 ID”
触发 GetCookie 方法
只有“ASP.NET SessionId”并且会话值仍然相同
感谢您的帮助。
I've been searching a solution for this problem but i couldn't have one. By the way i can't understand the reason of my problem.
The problem is:
My web application has a login page and gets logged user id from cookie. It worked before but 5-6 days ago because of something changed it didn't worked with IE. Now it doesn't work with any browser.
I can see the cookie in Chrome. When looked with Internet Explorer Developer Tool sometimes the cookie written but still can't read by IE
My web app is on Windows Server 2008 R2 BTW
Set my web.config:
<httpCookies domain=".domainname.com" httpOnlyCookies="false" requireSSL="false" />
Here is my SetCookie code
<!-- language: c# -->
string uId = "userID";
DateTime expireDate = DateTime.Now.AddDays(3);
HttpContext.Current.Response.Cookies["cookieName"]["uID"] = uId;
HttpCookie aCookie = new HttpCookie("cookieName");
aCookie.Values["uID"] = uId;
aCookie.Path = "/";
aCookie.Expires = expireDate;
aCookie.HttpOnly = false;
aCookie.Domain = "domainname.com";
aCookie.Name = "cookieName";
HttpContext.Current.Response.Cookies.Add(aCookie);
And this GetCookie code
<!-- language: c# -->
if (HttpContext.Current.Request.Cookies["cookieName"] != null)
{
System.Collections.Specialized.NameValueCollection UserInfoCookieCollection;
UserInfoCookieCollection = HttpContext.Current.Request.Cookies["cookieName"].Values;
userID = HttpContext.Current.Server.HtmlEncode(UserInfoCookieCollection["uID"]);
}
The scenario is:
trying to log in
SetCookie method triggered
End of SetCookie method there are two cookies "cookieName" and
"ASP.NET SessionId"GetCookie method triggered
There is only "ASP.NET SessionId" and session value still same
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个把我难住了。但设法解决如下。因此基本上将到期时间设置为初始化程序的一部分是行不通的。将 cookie 添加到响应对象后设置它就可以了!
This one had me stumped. But managed to solve it as follows. So basically setting the expiry as part of the initialiser does not work. Setting it after adding the cookie to the response object works!
我的问题解决了。将我的代码更改为这样
My problem solved. Changed my code to this