现在有一个方法btnLogin_Click,我在其中存了票证,存过后在这个方法中去取User.Identity.Name==“”,
但是在另外其他方法中User.Identity.Name确有值,这是为什么啊?
第一次走那个setTickets然后票证理是有值的,但是一担走LbOutLogin_Click他过后,在往票证理去写东西都写不进去,存后即可取值是“”。在其他方法调用不是null。
/// <summary> /// 设置票据 /// </summary> /// <param name="userName">登录名</param> /// <param name="rules">组</param> /// <param name="expires">是否持续</param> public static void SetTicket(UserEntity user, string rules, bool expires) { string ticketName = user.ToArray(); SetTickets(ticketName, rules, expires); } public static void SetTickets(string ticketName, string rules, bool expires) { //暂时为永不过期 //expires = false; HttpContext context = HttpContext.Current; string userDate = rules; DateTime expiration = DateTime.Now.AddMinutes(20); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, // 版本号 ticketName, // 票据名 DateTime.Now, // 票据发出时间 DateTime.Now.AddYears(100), // 过期时间 true, // 是否持久 userDate// 存储在 Cookie 中的用户定义数据,这里用来存取用户组 ); // Encrypt the ticket string cookieStr = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieStr); if (!string.IsNullOrEmpty(FormsAuthentication.CookieDomain)) { cookie.Domain = FormsAuthentication.CookieDomain; cookie.Path = "/"; } else { //如果过期 if (expires) { cookie.Expires = expiration; } } context.Response.Cookies.Add(cookie); }
//退出 protected void LbOutLogin_Click(object sender, EventArgs e) { tblLoginAfter.Visible = false; tblLoginBefure.Visible = true; HttpContext context = HttpContext.Current; FormsAuthentication.SignOut(); HttpCookie c = context.Request.Cookies[FormsAuthentication.FormsCookieName]; if (c == null) { return; } c.Expires = DateTime.Now.AddMinutes(-20); context.Response.SetCookie(c); }
方法btnLogin_Click在哪啊大哥。。。能顺便把你的page_load处的代码贴一下吗
page_load没有任何代码