yl
2022-08-11 f692cf7b41027664c9a3bf405d3c70cf123a76dd
VueWebApi/Tools/ChannelActionFilterAttribute.cs
@@ -26,32 +26,43 @@
        /// <param name="actionContext"></param>
        public override void  OnActionExecuting(HttpActionContext actionContext)
        {
            User loginUser = JsonConvert.DeserializeObject<User>(HttpContext.Current.Session["User"].ToString());
            base.OnActionExecuting(actionContext);
            //如果存在
            if (redis.Get<User>("LoginUserID" + loginUser.userid, 0).userid.ToString() != "")
            try
            {
                //获取redis中当前用户信息
                User r_loginUser = redis.Get<User>("LoginUserID" + loginUser.userid, 0);
                //如果session中的用户usercode和redis中的用户usercode匹配
                if (r_loginUser.usercode.Equals(loginUser.usercode))
                var User = HttpContext.Current.Session["User"].ToString();
                User loginUser = JsonConvert.DeserializeObject<User>(User);
                base.OnActionExecuting(actionContext);
                //如果存在
                if (redis.Get<User>("LoginUserID"+loginUser.usertype + loginUser.userid, 0).userid.ToString() != "")
                {
                    //重置过期时间
                    redis.Set<User>("LoginUserID" + loginUser.userid, loginUser, redis.secondsTimeOut, 0);
                    //获取redis中当前用户信息
                    User r_loginUser = redis.Get<User>("LoginUserID"+ loginUser.usertype + loginUser.userid, 0);
                    //如果session中的用户guid和redis中的用户guid匹配
                    if (r_loginUser.guid.Equals(loginUser.guid))
                    {
                        //重置过期时间
                        redis.Set<User>("LoginUserID"+ loginUser.usertype + loginUser.userid, loginUser, redis.secondsTimeOut, 0);
                    }
                    else
                    {
                        mes.code = "303";
                        mes.Message = "登录已超时,请重新登录!";
                        actionContext.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mes), Encoding.GetEncoding("UTF-8"), "application/json") };
                    }
                }
                else
                else //如果redis登录用户中不存在当前用户
                {
                    mes.code = "300";
                    mes.Message = "账户已过期,请重新登录2!";
                    actionContext.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.Ambiguous, Content = new StringContent(JsonConvert.SerializeObject(mes), Encoding.GetEncoding("UTF-8"), "application/json") };
                    mes.code = "303";
                    mes.Message = "登录已超时,请重新登录!";
                    actionContext.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mes), Encoding.GetEncoding("UTF-8"), "application/json") };
                }
            }
            else //如果redis登录用户中不存在当前用户
            catch (Exception)
            {
                mes.code = "300";
                mes.Message = "账户已过期,请重新登录1!";
                actionContext.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.Ambiguous, Content = new StringContent(JsonConvert.SerializeObject(mes), Encoding.GetEncoding("UTF-8"), "application/json") };
                mes.code = "303";
                mes.Message = "登录已超时,请重新登录!";
                actionContext.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(mes), Encoding.GetEncoding("UTF-8"), "application/json") };
            }
        }  
    }
}