コード例 #1
0
ファイル: MemberBusiness.cs プロジェクト: hhy5277/JustSay
 /// <summary>
 /// 持久化登录
 /// </summary>
 /// <param name="member"></param>
 /// <param name="days"></param>
 /// <returns></returns>
 public bool PersistentLogon(Member member, int days)
 {
     //加入持久化的代码
     try
     {
         //当为非管理员帐户时
         if (member.RoleID > 3)
         {
             FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, member.ID.ToString(), DateTime.Now, DateTime.Now.AddDays(days), true, member.Role.RoleName);
             string     encTicket = FormsAuthentication.Encrypt(authTicket);
             HttpCookie cookie    = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
             cookie.Expires  = DateTime.Now.AddDays(days);
             cookie.HttpOnly = true;
             HttpContext.Current.Response.Cookies.Add(cookie);
         }
         else//当为管理员帐户时  设置为几小时
         {
             FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, member.ID.ToString(), DateTime.Now, DateTime.Now.AddHours(3), member.RoleID > 3, member.Role.RoleName);
             string     encTicket = FormsAuthentication.Encrypt(authTicket);
             HttpCookie cookie    = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
             cookie.Expires  = DateTime.Now.AddHours(3);
             cookie.HttpOnly = true;
             HttpContext.Current.Response.Cookies.Add(cookie);
         }
         CookieHelper.AddUserInfo(member.Email, days, member.ID, member.ShowName);
         return(true);
     }
     catch
     {
         return(false);
     }
 }