/// <summary> /// 登录成功之后,写cookie调用 /// </summary> /// <param name="item"></param> /// <returns></returns> public static bool Login(SoUser item) { bool flag = false; try { if (item == null) { flag = false; } else { //防止调用时不给赋值时间导致问题 item.DT = DateTime.Now; string hashCookieName = SM.Current.HashManager.Hash_MD5_16(cookieName); string jsonUser = JsonConvert.SerializeObject(item); //序列化 string cookieValue = jsonUser; string cookieValueCypher = SM.Current.CryptoManager.Encrypt(cookieValue); CookieHelper.WriteCookie(SoLogin.cookieDoman, hashCookieName, cookieValueCypher, SoLogin.expiresMinute, false); //添加用户登录昵称 CookieHelper.WriteCookie(SoLogin.cookieDoman, "NickName", item.NickName, SoLogin.expiresMinute, false); flag = true; } } catch { flag = false; } return(flag); }
/// <summary> /// 返回已经成功登录用户对象实体 /// </summary> /// <param name="hashCookieName"></param> /// <returns></returns> private static SoUser GetUserModelByHash(string hashCookieName) { SoUser model = null; try { HttpCookie cookie = CookieHelper.ReadCookie(hashCookieName); if (null == cookie) { return(null); } string cookieValue = cookie.Value; if (!string.IsNullOrEmpty(cookieValue)) { string cookieValuePlain = SM.Current.CryptoManager.Decrypt(cookieValue); //cookie value 原 model = JsonConvert.DeserializeObject <SoUser>(cookieValuePlain); //反序列化 } } catch { model = null; } return(model); }
/// <summary> /// 要用经过Hash运行的用户名判断用户是否登录成功 /// </summary> /// <returns>登录成功返回True 登录失败返回False</returns> private static string IsLoginByHash(string hashCookieName) { bool flag = false; string soLibID = string.Empty; try { string cookieName = hashCookieName; HttpCookie cookie = CookieHelper.ReadCookie(cookieName); if (null == cookie) { return(soLibID); } string cookieValue = cookie.Value; string cookieValuePlain = SM.Current.CryptoManager.Decrypt(cookieValue); //cookie value 原码 SoUser model = JsonConvert.DeserializeObject <SoUser>(cookieValuePlain); //反序列化 if (model == null) { return(soLibID); } else { flag = true; soLibID = SM.Current.CryptoManager.Encrypt(string.Format("{0}{1}", model.UserName, model.DT.ToString("yyyy-MM-dd HH:mm:ss"))); } //flag = false; //string clientIPNow = SM.Current.IPManager.GetIP(); //string clientIPCookie = model.IP; //if (clientIPNow != null && clientIPCookie != null) //{ // if (clientIPNow == clientIPCookie) // { // flag = true; // } //} } catch { flag = false; } if (!flag) { soLibID = string.Empty; } return(soLibID); }