在302重定向期间发回cookie是否有任何问题?例如,如果我创建一个return-to-url cookie并在同一个响应中重定向用户,那么任何(现代)浏览器都会忽略cookie吗?
我在将ASP.NET中的cookie传递给新URL时遇到问题.我像这样在响应中添加cookie:
Response.Cookies.Add(new HttpCookie("Username", Username.Text));
Run Code Online (Sandbox Code Playgroud)
然后我发出重定向:
Response.Redirect(returnURL);
在我重定向到的新页面上,cookie集合为空.我尝试像这样检索一个cookie:
Request.Cookies["Username"].Value;
任何人都可以想到为什么没有通过cookie?
编辑:
进一步的信息我忘了添加 - 在同一个浏览器会话中的第二次尝试,cookie通过重定向正确传递.
编辑#2:我发现如果我在重定向URL中使用"localhost"而不是实际的域名,那么首次登录时会正确传递cookie.因此,只有当重定向URL是实际的域名时才会起作用.奇怪.
我正在构建dnn模块,允许登录用户以其他用户身份登录.
但我这里有一些有线问题.
这是我注销当前用户并以另一个用户身份登录的方式:
UserInfo userInfo = UserController.GetUserById(portalId, userId);
if (userInfo != null)
{
DataCache.ClearUserCache(this.PortalSettings.PortalId, Context.User.Identity.Name);
if (Session["super_userId"] == null)
{
Session["super_userId"] = this.UserId;
Session["super_username"] = this.UserInfo.Username;
}
HttpCookie impersonatorCookie = new HttpCookie("cookieName");
impersonatorCookie.Expires = DateTime.Now.AddHours(1);
Response.Cookies.Add(impersonatorCookie);
Response.Cookies["cookieName"]["super_userId"] = this.UserId.ToString();
Response.Cookies["cookieName"]["super_username"] = this.UserInfo.Username;
PortalSecurity objPortalSecurity = new PortalSecurity();
objPortalSecurity.SignOut();
UserController.UserLogin(portalId, userInfo, this.PortalSettings.PortalName, Request.UserHostAddress, false);
Response.Redirect(Request.RawUrl, true);
}
Run Code Online (Sandbox Code Playgroud)
在PageLoad()我尝试从这个cookie读取值但它没有读取任何东西:
try
{
string super_userId = Request.Cookies["cookieName"]["super_userId"];
string super_username = Request.Cookies["cookieName"]["super_username"];
if (!String.IsNullOrEmpty(super_userId))
{
this.Visible = true;
this.lblSuperUsername.Text = Session["super_username"].ToString(); …Run Code Online (Sandbox Code Playgroud)