我正在使用FormsAuthentication来管理login.I使用我需要的一些用户数据设置cookie。
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
txtUserName.Text,
DateTime.Now,
DateTime.Now.AddMonths(1),
false,
roles,
FormsAuthentication.FormsCookiePath);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie =new HttpCookie(FormsAuthentication.FormsCookieName,
encryptedTicket);
authCookie.Expires = DateTime.Now.AddMonths(1);
Response.Cookies.Add(authCookie);
//doesn't work
//string url = FormsAuthentication.GetRedirectUrl(txtUserName.Text, false);
//Response.Redirect(url, false);
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false);
使用 FormsAuthentication.RedirectFromLoginPage 正确地重定向但是它设置了自己的cookie,其中空的用户数据覆盖了我的,而 Response.Redirect 根本没有重定向。 用提琴手监听我可以看到:
//WITH Response.Redirect
POST to /WebApp/logon.aspx Result: 302
Cookie: ASP.NET_SessionId=4p0wrvbssiccgai0tspxo3zk
Set-Cookie: WWAuthCookie=SMALLCOOKIE; expires=Fri, 23-May-2014 08:20:58 GMT; path=/
Transport Location: /OST_VS/default.aspx
GET /WebApp/default.aspx HTTP/1.1 Result: 302
Transport Location: /WebApp/Logon.aspx?ReturnUrl=%2fWebApp%2fdefault.aspx
GET /WebApp/Logon.aspx?ReturnUrl=%2fWebApp%2fdefault.aspx HTTP/1.1
这是 FormsAuthentication.RedirectFromLoginPage
POST /WebApp/Logon.aspx HTTP/1.1 Result: 302
Cookie: ASP.NET_SessionId=4p0wrvbssiccgai0tspxo3zk
Set-Cookie: WWAuthCookie=BIGCOOKIE; expires=Fri, 23-May-2014 08:27:33 GMT; path=/
Set-Cookie: WWAuthCookie=SMALLCOOKIE; path=/; HttpOnly
GET /WebApp/default.aspx HTTP/1.1 Result: 200
Transport Content-Encoding: gzip
你看到的第一件事就是使用FormsAuthentication.RedirectFromLoginPage,cookie被一个较小的cookie覆盖,第二件事就是使用Response,响应也是不同的。重定向它到了default.apsx但是又回来了吗? 我真的无法理解发生了什么。
答案 0 :(得分:0)
显然这与某些浏览器对Cookie的限制上限有关。我的用户数据超过4k 导致其无法重定向,因为浏览器已被拒绝由于FormsAuthentication.RedirectFromLoginPage没有userdata,因此它更小,可以重定向。
所以我所做的就是使用Gzip压缩我的userdata字符串以使其适合(现在它位于2k)。
我认为为userdata设置单独的cookie也可以。