public RedisSetKeyExpireTimeResult SetRedisKeyExpireTime(string keyName, int timeSeconds)
        {
            RedisSetKeyExpireTimeResult httpResponse = new RedisSetKeyExpireTimeResult();

            httpResponse.Date                = DateTime.Now;
            httpResponse.KeyName             = keyName;
            httpResponse.ExpiryTimeInSeconds = timeSeconds;
            int statusCode = (int)HttpStatusCode.OK;

            if (RedisHelpers.SetKeyExpireTime(keyName, timeSeconds))
            {
                httpResponse.Message = "Set expiry time success";
                // trick: set time interval using seconds
                TimeSpan interval = new TimeSpan(0, 0, timeSeconds);
                httpResponse.ExpectedExpiryDate = DateTime.Now + interval;
            }
            else
            {
                statusCode           = (int)HttpStatusCode.InternalServerError;
                httpResponse.Message = "Something not good happened...";
            }
            this.HttpContext.Response.StatusCode = statusCode;
            httpResponse.StatusCode = statusCode;
            return(httpResponse);
        }
        public static bool CompareVerificationCode(string emailAddress, int verificationCodeToCheck)
        {
            string verificationCodeString = RedisHelpers.GetString("earth_fusion_" + emailAddress + "_verification_code");

            if (verificationCodeString == verificationCodeToCheck.ToString())
            {
                // make verification code related keys expire
                RedisHelpers.SetKeyExpireTime("earth_fusion_" + emailAddress + "_verification_code", 1);
                RedisHelpers.SetKeyExpireTime("earth_fusion_" + emailAddress + "_verification_code_sent", 1);
                return(true);
            }
            return(false);
        }
Пример #3
0
        public Session GetLoginSession(string username, string password)
        {
            Session httpResponse = new Session();

            Logging.Info("request", "Received request for GetLoginSession");
            Logging.Info("GetLoginSession", "username: "******"GetLoginSession", "Begins.");
            UserInformation userInformation = SessionHelpers.Login(username, password);

            if (userInformation == null)
            {
                statusCode           = (int)HttpStatusCode.Forbidden;
                httpResponse.Message = "boom";
            }
            else
            {
                httpResponse.Message  = "good";
                httpResponse.username = userInformation.userName;
                Random random = new System.Random();
                string sessionId;
                while (true)
                {
                    sessionId = GenericHelpers.CreateMD5(random.Next(114514, 1919810).ToString());
                    string sessionKeyName = "EARTH_FUSION_SESSION_" + sessionId;
                    if (RedisHelpers.GetString(sessionKeyName) == null)
                    {
                        RedisHelpers.SetString(sessionKeyName, userInformation.userId.ToString());
                        // three hour
                        RedisHelpers.SetKeyExpireTime(sessionKeyName, 3 * 60 * 60);
                        break;
                    }
                }
                httpResponse.sessionId = sessionId;
            }
            httpResponse.StatusCode = statusCode;
            this.HttpContext.Response.StatusCode = statusCode;
            Logging.Info("request", "Reponse returned for GetLoginSession");
            return(httpResponse);
        }
        public static bool RequestVerificationCode(string emailAddress)
        {
            string verificationCodeSentString = RedisHelpers.GetString("earth_fusion_" + emailAddress + "_verification_code_sent");

            if (verificationCodeSentString != null)
            {
                // already has an code.
                return(false);
            }
            Random random = new System.Random();
            // 6 digit
            int    verificationCode       = random.Next(100000, 999999);
            string verificationCodeString = verificationCode.ToString();

            SendgridHelpers.SendVerificationCodeTask(emailAddress, "User", verificationCodeString);
            // set redis
            RedisHelpers.SetString("earth_fusion_" + emailAddress + "_verification_code", verificationCodeString);
            RedisHelpers.SetString("earth_fusion_" + emailAddress + "_verification_code_sent", "1");
            // set verification code timeout: 600s (10 min)
            RedisHelpers.SetKeyExpireTime("earth_fusion_" + emailAddress + "_verification_code", 600);
            // set timeout for when can user request another verification code: 60 (1 min)
            RedisHelpers.SetKeyExpireTime("earth_fusion_" + emailAddress + "_verification_code_sent", 60);
            return(true);
        }