我正在尝试从auth ticket中读取用户名(TESTTEST
)
-----登录页面------
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
"TESTTEST",
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
String.Empty,
FormsAuthentication.FormsCookiePath);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie authCookie = new HttpCookie(
FormsAuthentication.FormsCookieName,
encryptedTicket);
authCookie.Secure = true;
Response.Cookies.Add(authCookie);
FormsAuthentication.RedirectFromLoginPage("User", false);
------受保护的页面-------
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
Label1.Text = ticket.Name;
结果:标签文字为"USER"
而非"TESTTEST"
答案 0 :(得分:1)
FormsAuthentication.RedirectFromLoginPage
创建一个带有提供名称的新故障单(在您的情况下为“User”)。来自http://msdn.microsoft.com/en-us/library/ka5ffkce.aspx
如果CookiesSupported属性为true,并且ReturnUrl变量在当前应用程序中或EnableCrossAppRedirects属性为true,则RedirectFromLoginPage方法会发出身份验证票证并使用SetAuthCookie方法将其置于默认cookie中。
您可能希望使用以下代码而不是RedirectFromLoginPage
returnUrl = FormsAuthentication.GetRedirectUrl(userName, false);
HttpContext.Current.Response.Redirect(returnUrl);