我在将ASP.NET中的cookie传递给新URL时遇到问题.我像这样在响应中添加cookie:
- Response.Cookies.Add(new HttpCookie("Username",Username.Text));
然后我发出重定向:
的Response.Redirect(RETURNURL);
在我重定向到的新页面上,cookie集合为空.我尝试像这样检索一个cookie:
Request.Cookies时[ “用户名”]值.
任何人都可以想到为什么没有通过cookie?
编辑:
进一步的信息我忘了添加 – 在同一个浏览器会话中的第二次尝试,cookie通过重定向正确传递.
编辑#2:我发现如果我在重定向URL中使用“localhost”而不是实际的域名,那么首次登录时会正确传递cookie.因此,只有当重定向URL是实际的域名时才会起作用.奇怪.
解决方法
- protected void Page_Load(object sender,EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- if (Request.QueryString["AcceptsCookies"] == null)
- {
- Response.Cookies["TestCookie"].Value = "ok";
- Response.Cookies["TestCookie"].Expires =
- DateTime.Now.AddMinutes(1);
- Response.Redirect("TestForCookies.aspx?redirect=" +
- Server.UrlEncode(Request.Url.ToString()));
- }
- else
- {
- Label1.Text = "Accept cookies = " +
- Server.UrlEncode(
- Request.QueryString["AcceptsCookies"]);
- }
- }
- }
This link将帮助您了解C#中的读写cookie.
此外,如果您熟悉VB而不是C#,@L_301_1@将非常有用.