using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.Script.Serialization; namespace FangYar.Common { /// /// 用户帮助类:获取Cookie里的用户对象 /// public class UserHelper { /// /// 获取保存在cookie里的用户对象 /// public static FangYar.Model.LoginUserModel GetUser(HttpContext context) { try { //if (context.Request.IsAuthenticated) //{ // FormsIdentity id = (FormsIdentity)context.User.Identity; // FormsAuthenticationTicket tickets = id.Ticket; // //反序列化获取票证里序列化的用户对象 // FangYar.Model.LoginUserModel userFromCookie = new JavaScriptSerializer().Deserialize(tickets.UserData); // return userFromCookie; //} //else // return null; var cook = context.Request.Cookies; var cookInfo = cook["kn_root_cookie"]; if (cookInfo != null) { var str = cookInfo.Value; //判断是否存在登录缓存信息 if (!string.IsNullOrWhiteSpace(str)) { var usersUid = str.Substring(str.IndexOf("\"usersUid\":\"") + 12); usersUid = usersUid.Substring(0, usersUid.IndexOf("\",")); FangYar.Model.LoginUserModel userFromCookie = new JavaScriptSerializer().Deserialize(str); return userFromCookie; } } } catch (Exception ex) { return null; } return null; } } }