Asp.net HttpCookie 读/写问题

发布于 2024-12-05 17:14:26 字数 1609 浏览 1 评论 0原文

我一直在寻找解决这个问题的方法,但我找不到。顺便说一句,我不明白我的问题的原因。

问题是:

我的 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 技术交流群。

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

发布评论

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

评论(2

同展鸳鸯锦 2024-12-12 17:14:26

这个把我难住了。但设法解决如下。因此基本上将到期时间设置为初始化程序的一部分是行不通的。将 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!

enter image description here

乱世争霸 2024-12-12 17:14:26

我的问题解决了。将我的代码更改为这样

            string uId = "userID";
            DateTime expireDate = DateTime.Now.AddDays(3);

            var httpCookie = HttpContext.Current.Response.Cookies["cookieName"];
            if (httpCookie != null)
            {
                httpCookie["uID"] = uId;

                HttpContext.Current.Response.Cookies.Add(httpCookie);
            }
            else
            {
                HttpCookie aCookie = new HttpCookie("cookieName");
                aCookie.Values["uID"] = uId;
                aCookie.Expires = expireDate;

                HttpContext.Current.Response.Cookies.Add(aCookie);
            }

My problem solved. Changed my code to this

            string uId = "userID";
            DateTime expireDate = DateTime.Now.AddDays(3);

            var httpCookie = HttpContext.Current.Response.Cookies["cookieName"];
            if (httpCookie != null)
            {
                httpCookie["uID"] = uId;

                HttpContext.Current.Response.Cookies.Add(httpCookie);
            }
            else
            {
                HttpCookie aCookie = new HttpCookie("cookieName");
                aCookie.Values["uID"] = uId;
                aCookie.Expires = expireDate;

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