/// <summary> /// 获取用户对象 by Cookie /// </summary> /// <returns></returns> public static SesionUser GetUserFromCookie() { SesionUser user = new SesionUser(); if (HttpContext.Current.Session != null) { if (HttpContext.Current.Session["UserID"] != null && HttpContext.Current.Session["RoleID"] != null) { user.UserID = HttpContext.Current.Session["UserID"] == null ? 0 : int.Parse(HttpContext.Current.Session["UserID"].ToString()); user.Username = HttpContext.Current.Session["Username"] == null ? "" : HttpContext.Current.Session["Username"].ToString(); user.RoleID = HttpContext.Current.Session["RoleID"] == null ? -1 : int.Parse(HttpContext.Current.Session["RoleID"].ToString()); user.IsBand = HttpContext.Current.Session["IsBand"] == null ? 0 : int.Parse(HttpContext.Current.Session["IsBand"].ToString()); user.AgentID = HttpContext.Current.Session["AgentID"] == null ? 0 : int.Parse(HttpContext.Current.Session["AgentID"].ToString()); user.VipID = HttpContext.Current.Session["VipID"] == null ? 0 : int.Parse(HttpContext.Current.Session["VipID"].ToString()); SetUserCookie(user); return(user); } else { return(null); } } else { return(null); } }
/// <summary> /// 设置用户 Cookie /// </summary> /// <param name="user"></param> public static void SetUserCookie(SesionUser user) { HttpContext.Current.Session["UserID"] = user.UserID; HttpContext.Current.Session["Username"] = user.Username; HttpContext.Current.Session["RoleID"] = user.RoleID; HttpContext.Current.Session["IsBand"] = user.IsBand; HttpContext.Current.Session["AgentID"] = user.AgentID; HttpContext.Current.Session["VipID"] = user.VipID; HttpContext.Current.Session.Timeout = Convert.ToInt32(ConfigHelper.GetAppSetting("SessionExpire")); }
/// <summary> /// 检查用户登录状态 /// </summary> /// <returns></returns> public static bool CheckedUserLogon() { SesionUser user = AdminCookie.GetUserFromCookie(); if (user == null || user.UserID <= 0 || user.RoleID <= 0) { return(false); } else { AdminCookie.SetUserCookie(user); } return(true); }