public dynamic GetAuthorizationCode(string code, string state, string clientId, string redirectUri = "")
        {
            string redisCode = RedisCacheHelper.Get <string>(tenantId, "GetCode" + clientId) ?? "";

            returnData.Note = state;
            if (string.IsNullOrEmpty(redisCode))
            {
                returnData.Massage = "code过期";
                return(returnData);
            }
            if (!redisCode.Equals(code))
            {
                returnData.Massage = "code有误";
                return(returnData);
            }
            string accesstoken = Guid.NewGuid().ToString("N");

            returnData.Massage = accesstoken;
            RedisCacheHelper.Add(tenantId, "GetAuthorizationCode" + clientId, accesstoken, DateTime.Now.AddMinutes(accesstokenValidTime));

            RedisCacheHelper.Remove(tenantId, "GetCode" + clientId);
            if (!string.IsNullOrEmpty(redirectUri))
            {
                HttpContext.Current.Response.Redirect(redirectUri + "?code=" + code + "&state=" + state, true);
            }
            return(returnData);
        }
Exemple #2
0
        public void RedisRemove()
        {
            RedisCacheHelper.Remove("kim2");
            string str = RedisCacheHelper.Get <string>("kim2");

            Assert.AreEqual(null, str);
        }
Exemple #3
0
        public static void RemoveCurrentLoginInfo()
        {
            int userId = 0;

            if (TryGetCurrentUserID(out userId))
            {
                var Session = HttpContext.Current.Session;
                Session[SessionKey] = null;
                RedisCacheHelper.Remove(GetCacheKey(userId.ToString()));
            }
        }
Exemple #4
0
        public static void RefreshCache(HttpSessionStateBase Session, LoginUserModel longinuser)
        {
            string cacheKey = GetCacheKey(longinuser.SupplierUserInfo.ID.ToString());

            RedisCacheHelper.Remove(cacheKey);
            Session[SessionKey] = null;
            Session[SessionKey] = longinuser.SupplierUserInfo.ID;
            longinuser.SupplierUserInfo.UserID    = longinuser.SupplierUserInfo.ID;
            longinuser.SupplierUserInfo.CacheTime = DateTime.Now;
            RedisCacheHelper.Add <LoginUserModel>(cacheKey, longinuser, DateTime.Now.AddMinutes(Session.Timeout));
        }
Exemple #5
0
        public static void RefreshCache(LoginUserModel longinuser)
        {
            var    Session  = HttpContext.Current.Session;
            string cacheKey = GetCacheKey(longinuser.AdminUserInfo.id);

            RedisCacheHelper.Remove(cacheKey);
            Session[SessionKey] = null;
            Session[SessionKey] = longinuser.AdminUserInfo.id;
            longinuser.AdminUserInfo.CacheTime = DateTime.Now;
            RedisCacheHelper.Add <LoginUserModel>(cacheKey, longinuser, DateTime.Now.AddMinutes(Session.Timeout));
        }
Exemple #6
0
        public static void RemoveCurrentLoginInfo()
        {
            var userId = 0;

            if (TryGetCurrentUserID(out userId))
            {
                var Session = HttpContext.Current.Session;
                Session[SessionKey] = null;
                var key = GetCacheKey(userId);
                RedisCacheHelper.Remove(key);
            }
        }
Exemple #7
0
        public static void RemoveLoginSession()
        {
            var session = HttpContext.Current.Session;

            if (session[ConstClass.SessionKeyMLoginUser] != null)
            {
                var id = session[ConstClass.SessionKeyMLoginUser].As(0);
                RedisCacheHelper.Remove(GetLoginUserRedisKey(id));
                session[ConstClass.SessionKeyMLoginUser] = null;
                CookieHelper.ClearCookie(ConstClass.LoginUserCookieKey);
            }
        }
Exemple #8
0
        public ResponseBaseModel <dynamic> CheckCodeBaseStr(string token)
        {
            var yzm = new YzmHelper();

            yzm.CreateImage();
            var          code = yzm.Text;
            Bitmap       img  = yzm.Image;
            MemoryStream ms   = new MemoryStream();

            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            string str = Convert.ToBase64String(ms.ToArray());

            RedisCacheHelper.Remove(RedisCacheKey.AuthCheckCodeKey + token);
            RedisCacheHelper.AddSet(RedisCacheKey.AuthCheckCodeKey + token, code, DateTime.Now.AddMinutes(3));
            return(new ResponseBaseModel <dynamic> {
                ResultCode = ResponceCodeEnum.Success, Message = "", Data = "data:image/png;base64," + str
            });
        }
Exemple #9
0
        public static void RefreshSession(LoginUserModel user)
        {
            var session = HttpContext.Current.Session;

            if (user != null)
            {
                if (user.LoginTime.AddMinutes(session.Timeout - 5) < DateTime.Now)
                {
                    RedisCacheHelper.Remove(GetLoginUserRedisKey(user.UserID));
                    session[ConstClass.SessionKeyMLoginUser] = null;
                    session[ConstClass.SessionKeyMLoginUser] = user.UserID;

                    //user.LoginTime = DateTime.Now;

                    RedisCacheHelper.Add(GetLoginUserRedisKey(user.UserID), user, session.Timeout);
                }
            }
        }
Exemple #10
0
        /// <summary>
        /// 检查登陆状态
        /// </summary>
        /// <param name="userModel"></param>
        /// <returns></returns>
        private static bool CheckSessionBase(out LoginUserModel userModel)
        {
            userModel = null;
            if (System.Web.HttpContext.Current.Session != null &&
                System.Web.HttpContext.Current.Session[ConstClass.SessionKeyMLoginUser] != null)
            {
                int userId = System.Web.HttpContext.Current.Session[ConstClass.SessionKeyMLoginUser].As(0);

                try
                {
                    if (userId <= 0)
                    {
                        System.Web.HttpContext.Current.Session[ConstClass.SessionKeyMLoginUser] = null;
                        System.Web.HttpContext.Current.Session.Clear();
                        return(false);
                    }
                    var tempuser = RedisCacheHelper.AutoCache <LoginUserModel>("", GetLoginUserRedisKey(userId), () => accountBll.GetUserById(userId).AsLoginUserModel(), ConfigHelper.SessionExpireMinutes);
                    if (tempuser == null || tempuser.Status != 1)
                    {
                        //清错误缓存
                        RedisCacheHelper.Remove(GetLoginUserRedisKey(userId));
                        System.Web.HttpContext.Current.Session[ConstClass.SessionKeyMLoginUser] = null;
                        System.Web.HttpContext.Current.Session.Clear();
                        return(false);
                    }
                    userModel = tempuser;
                    return(true);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    RedisCacheHelper.Remove(GetLoginUserRedisKey(userId));
                    System.Web.HttpContext.Current.Session[ConstClass.SessionKeyMLoginUser] = null;
                    return(false);
                }
            }

            return(false);
        }
Exemple #11
0
        public ResponseBaseModel <dynamic> BindWeChatUser(string name, string password, string checkcode, string token)
        {
            if (string.IsNullOrEmpty(token))
            {
                return(new ResponseBaseModel <dynamic> {
                    ResultCode = ResponceCodeEnum.Fail, Message = "token失效"
                });
            }
            var userData = RedisCacheHelper.Get <WeChatAccountModel>(RedisCacheKey.AuthTokenKey + token);

            if (userData == null || userData.AccountId > 0)
            {
                return(new ResponseBaseModel <dynamic> {
                    ResultCode = ResponceCodeEnum.Fail, Message = "token失效或者已经绑定过信息"
                });
            }

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
            {
                return(new ResponseBaseModel <dynamic> {
                    ResultCode = ResponceCodeEnum.Fail, Message = "用户名和密码不能为空"
                });
            }

            if (string.IsNullOrEmpty(checkcode))
            {
                return(new ResponseBaseModel <dynamic> {
                    ResultCode = ResponceCodeEnum.Fail, Message = "验证码不能为空"
                });
            }
            var oldCheckCode = RedisCacheHelper.Get <string>(RedisCacheKey.AuthCheckCodeKey + token);

            RedisCacheHelper.Remove(RedisCacheKey.AuthCheckCodeKey + token);
            if (oldCheckCode != checkcode)
            {
                return(new ResponseBaseModel <dynamic> {
                    ResultCode = ResponceCodeEnum.Fail, Message = "验证码错误"
                });
            }
            var accountService = new AccountService();

            password = AesHelper.AesEncrypt(password);
            var loginInfo = accountService.GetSysUsersByUserName(name)?.FirstOrDefault();

            //没有该用户则注册一个用户
            if (loginInfo == null || loginInfo.Id < 1)
            {
                var newModel = new SysUser
                {
                    Birthday   = "1900-01-01 00:00:00",
                    CreateTime = DateTime.Now,
                    IsDel      = FlagEnum.HadZore,
                    CreateAuth = 1,
                    HeadUrl    = userData.AvatarUrl,
                    Password   = password,
                    Sex        = EnumHelper.GetEnumByValue <SexEnum>(userData.Gender),
                    UpdateAuth = 1,
                    UpdateTime = DateTime.Now,
                    UserType   = UserTypeEnum.Usually,
                    UserName   = name
                };
                var resultId = accountService.InsertWeChatUserAndBind(newModel, userData.OpenId);
                //处理成功
                if (resultId > 0)
                {
                    userData.AccountId = resultId;
                    RedisCacheHelper.AddSet(RedisCacheKey.AuthTokenKey + token, userData, DateTime.Now.AddDays(1));
                }
                var resultModel = new WeChatAuthResponseModel
                {
                    Token        = token,
                    CodeTimeSpan = DateTime.Now.AddDays(1).ToLongTimeString(),
                    AvatarUrl    = userData.AvatarUrl,
                    AccountId    = resultId
                };
                return(new ResponseBaseModel <dynamic> {
                    ResultCode = ResponceCodeEnum.Success, Message = "", Data = resultModel
                });
            }
            //有该用户,查看是否已经绑定过别人
            else
            {
                if (loginInfo.Password != password)
                {
                    return(new ResponseBaseModel <dynamic> {
                        ResultCode = ResponceCodeEnum.Fail, Message = "密码错误"
                    });
                }
                var wechatServer = new WechatAccountService();
                var otherData    = wechatServer.GetByAccountId(loginInfo.Id);
                //有人已经绑定了
                if (otherData != null)
                {
                    if (otherData.OpenId == userData.OpenId)
                    {
                        var resultModeltemp = new WeChatAuthResponseModel
                        {
                            Token        = token,
                            CodeTimeSpan = DateTime.Now.AddDays(1).ToLongTimeString(),
                            AvatarUrl    = userData.AvatarUrl,
                            AccountId    = loginInfo.Id
                        };
                        RedisCacheHelper.AddSet(RedisCacheKey.AuthTokenKey + token, userData, DateTime.Now.AddDays(1));
                        return(new ResponseBaseModel <dynamic> {
                            ResultCode = ResponceCodeEnum.Success, Message = "", Data = resultModeltemp
                        });
                    }
                    return(new ResponseBaseModel <dynamic> {
                        ResultCode = ResponceCodeEnum.Fail, Message = "非法绑定"
                    });
                }
                userData.AccountId = loginInfo.Id;
                var oldModel = wechatServer.GetByOpenId(userData.OpenId);
                if (oldModel == null)
                {
                    wechatServer.SaveModel(userData);
                }
                else
                {
                    oldModel.AccountId = userData.AccountId;
                    wechatServer.SaveModel(oldModel);
                }
                RedisCacheHelper.AddSet(RedisCacheKey.AuthTokenKey + token, userData, DateTime.Now.AddDays(1));
                var resultModel = new WeChatAuthResponseModel
                {
                    Token        = token,
                    CodeTimeSpan = DateTime.Now.AddDays(1).ToLongTimeString(),
                    AvatarUrl    = userData.AvatarUrl,
                    AccountId    = loginInfo.Id
                };
                return(new ResponseBaseModel <dynamic> {
                    ResultCode = ResponceCodeEnum.Success, Message = "", Data = resultModel
                });
            }
        }
Exemple #12
0
        public static void RemoveLoginInfo(int userId)
        {
            var key = GetCacheKey(userId.ToString());

            RedisCacheHelper.Remove(key);
        }
Exemple #13
0
 /// <summary>
 /// 停用 Token
 /// </summary>
 /// <param name="token">Token</param>
 /// <returns></returns>
 public async Task DeactivateAsync(string token)
 {
     redisCacheHelper.Remove(token);
 }