예제 #1
0
        //RedisSession rs = new RedisSession();

        public bool CheckIpAttemptTimes(int maxAtttemptTimes, int expiredMinutes)
        {
            bool      result = false;
            UserIp    uip;
            GetUserIp GetIp  = new GetUserIp();
            string    userIp = GetIp.GetLoginIp();

            using (IRedisClient redisClent = RedisHelper.clientManager.GetClient())
            {
                if (redisClent.Get <UserIp>(userIp) != null)
                {
                    uip = redisClent.Get <UserIp>(userIp);
                    if (uip.AttemptTime++ > maxAtttemptTimes)
                    {
                        result = false;
                    }
                    else
                    {
                        uip.AttemptTime++;
                        uip.LastAttempt = DateTime.Now;
                        if (uip.LastAttempt.Subtract(uip.FirstAttempt).TotalMinutes > expiredMinutes)
                        {
                            uip.FirstAttempt = uip.LastAttempt;
                            uip.AttemptTime  = 1;
                            redisClent.Set <UserIp>(userIp, uip);
                        }
                        else
                        {
                            redisClent.Set <UserIp>(userIp, uip);
                        }
                        result = true;
                    }
                }
                else
                {
                    uip              = new UserIp();
                    uip.Ip           = userIp;
                    uip.FirstAttempt = DateTime.Now;
                    uip.LastAttempt  = uip.FirstAttempt;
                    uip.AttemptTime  = 1;
                    redisClent.Set <UserIp>(userIp, uip);
                    result = true;
                }
            }
            return(result);
        }
예제 #2
0
        public bool CheckIpAttemptTimes(int maxAtttemptTimes, int expiredMinutes)
        {
            GetUserIp GetIp  = new GetUserIp();
            string    userIp = GetIp.GetLoginIp();

            if (MemcacheHelper.Get(userIp) != null)
            {
                UserIp uip = SerializerHelper.DeserializeToObject <UserIp>(MemcacheHelper.Get(userIp).ToString());
                if (uip.AttemptTime++ > maxAtttemptTimes)
                {
                    return(false);
                }
                else
                {
                    uip.AttemptTime++;
                    uip.LastAttempt = DateTime.Now;
                    if (uip.LastAttempt.Subtract(uip.FirstAttempt).TotalMinutes > expiredMinutes)
                    {
                        uip.FirstAttempt = uip.LastAttempt;
                        uip.AttemptTime  = 1;
                        MemcacheHelper.Set(userIp, SerializerHelper.SerializerToString(uip), uip.FirstAttempt.AddMinutes(expiredMinutes));
                    }
                    else
                    {
                        MemcacheHelper.Set(userIp, SerializerHelper.SerializerToString(uip), uip.FirstAttempt.AddMinutes(expiredMinutes));
                    }
                    return(true);
                }
            }
            else
            {
                UserIp uip = new UserIp();
                uip.Ip           = userIp;
                uip.FirstAttempt = DateTime.Now;
                uip.LastAttempt  = uip.FirstAttempt;
                uip.AttemptTime  = 1;
                MemcacheHelper.Set(userIp, SerializerHelper.SerializerToString(uip), uip.FirstAttempt.AddMinutes(expiredMinutes));
                return(true);
            }
        }