基于.NET/Webforms的我的网络应用程序无法正常工作,因为在重定向到同一应用程序域上的页面后会丢失设置cookie的价值。但这个应用程序之前工作得很好问题如下:
在登录时设置cookie:
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
. . .
string userData = JsonConvert.SerializeObject(eng);
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
UserName.Text,
DateTime.Now,
DateTime.Now.AddMinutes(15),
false,
userData);
string encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
Response.Cookies.Add(faCookie);
Response.Redirect("~/View_Id");
}
}
else
{
FailureText.Text = "Invalid username or password.";
ErrorMessage.Visible = true;
}
}
设置新的cookie工作正常,但重定向后值会丢失。 Global.FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
上需要该值。
答案 0 :(得分:0)
好的狐狸,在读完我发现的cookie后,设置的用户数据太大(超过允许的4k) - 因此设置的cookie值将丢失或覆盖为null。将用户数据的大小减小到最大。限制4k是解决方案!