/// <summary> /// 检测用户是否登陆 /// </summary> /// <returns></returns> public static bool CheckUserIsLogin() { string stamp = EncryptCookies.GetValue(CookieConfig.StampCkName); string userId = EncryptCookies.GetValue(CookieConfig.UserIdCkName); string tokenvalue = EncryptCookies.GetValue(CookieConfig.TokenCkName); if (string.IsNullOrEmpty(stamp)) { return(false); } if (string.IsNullOrEmpty(userId)) { return(false); } if (string.IsNullOrEmpty(tokenvalue)) { return(false); } //计算token IToken token = new LoginToken(userId, Convert.ToInt64(stamp), TokenConfig.LoginTokenKey); //对比cookie中的和重新计算的 if (tokenvalue.ToUpper() == token.GetToken().ToUpper()) { return(true); } return(false); }
/// <summary> /// 用户检测 /// </summary> /// <param name="filterContext"></param> protected override void OnActionExecuting(ActionExecutingContext filterContext) { //提取所有验证 RequestRoleAuthorize = new RequestRoleAuthorize(filterContext.Controller, filterContext.ActionDescriptor); //cookie 中json 信息 CurrentUserInfo = UsersLogin.CheckUserIsLoginAndGetUserInfo(); //cookie 中校验过的userid string userId = EncryptCookies.GetValue(CookieConfig.UserIdCkName); //正常登陆 if (CurrentUserInfo != null && CurrentUserInfo.Id.ToString(CultureInfo.InvariantCulture) == userId) { UserIsLogin = true; RequestRoleAuthorize = new RequestRoleAuthorize(filterContext.Controller, filterContext.ActionDescriptor); if (RequestRoleAuthorize.HaveVerification) { #region 用户信息 var user = Users.GetUserById(CurrentUserInfo.Id); if (user == null || user.IsNull) { UsersLogin.LogOut(); const string msg = "您的账户已不存在,如有疑问请联系管理员!"; filterContext.Result = AutoResult(msg, false, Url.Action("Warning", "Tips", new { msg })); } #endregion #region 校验禁用状态 if (RequestRoleAuthorize.IsDisableVerification && user.Disable) { const string msg = "您的账户已被禁用,如有疑问请联系管理员!"; filterContext.Result = AutoResult(msg, false, Url.Action("Warning", "Tips", new { msg })); } #endregion #region 需要邮箱激活验证 if (RequestRoleAuthorize.IsEmailActivatedVerification && !user.EmailStatus) { const string msg = "请激活邮箱后再进行操作,如有疑问请联系管理员!"; filterContext.Result = AutoResult(msg, false, Url.Action("activate", "user"), true); } #endregion } } else if (RequestRoleAuthorize.HaveVerification) //未登录时 如果需要权限判断 则跳出 { UsersLogin.LogOut(); //无法得到用户信息就登出清空原有cookie再次登陆 //需要验证 filterContext.Result = AutoResult("请重新登陆后操作!", false, Url.Action("Login", "Vip")); } #region 全局数据 ViewData["CurrentPartUserInfo"] = CurrentUserInfo; ViewData["WebName"] = WebSysConfig.WebName; ViewData["IsLogin"] = UserIsLogin; #endregion }
/// <summary> /// 得到当前登录用户信息 /// </summary> /// <returns></returns> public static UserInfo GetCurrentLoginUserInfo() { if (!CheckUserIsLogin()) { return(null); } string userId = EncryptCookies.GetValue(CookieConfig.UserIdCkName); return(DataProvider.Users.GetUserInfo(int.Parse(userId))); }
/// <summary> /// 检测用户是否登陆并返回用户信息(解析cookie信息,该信息用于展示,不能用于业务逻辑,业务逻辑请使用id获取用户信息) /// </summary> /// <returns></returns> public static UserVModel CheckUserIsLoginAndGetUserInfo() { string stamp = EncryptCookies.GetValue(CookieConfig.StampCkName); string userId = EncryptCookies.GetValue(CookieConfig.UserIdCkName); string tokenvalue = EncryptCookies.GetValue(CookieConfig.TokenCkName); string userOther = EncryptCookies.GetValue(CookieConfig.UserOther);//其它信息 if (string.IsNullOrEmpty(stamp)) { return(null); } if (string.IsNullOrEmpty(userId)) { return(null); } if (string.IsNullOrEmpty(tokenvalue)) { return(null); } //计算token IToken token = new LoginToken(userId, Convert.ToInt64(stamp), TokenConfig.LoginTokenKey); //对比cookie中的和重新计算的 if (tokenvalue.ToUpper() == token.GetToken().ToUpper()) { try { //解析信息 var user = JsonConvert.DeserializeObject <UserVModel>(userOther); return(user); } catch (Exception) { LogOut(); return(null); } } return(null); }