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);
        }
예제 #2
0
        public void GetValueDateTimeIsNull2()
        {
            Type   type  = typeof(DateTime?);
            string value = "ABCDEF123";

            var actual = RedisHelpers.GetValue(type, value);

            Assert.IsNull(actual);
        }
예제 #3
0
        public void GetValueEnumIsNull2()
        {
            Type   type  = typeof(TestStatus);
            string value = "ABC#$";

            var actual = RedisHelpers.GetValue(type, value);

            Assert.IsNull(actual);
        }
예제 #4
0
        public void GetValueInt64IsNull2()
        {
            Type   type  = typeof(long?);
            string value = "ABC123";

            var actual = RedisHelpers.GetValue(type, value);

            Assert.Null(actual);
        }
예제 #5
0
        public void GetValueInt32IsNull()
        {
            Type   type  = typeof(int?);
            string value = null;

            var actual = RedisHelpers.GetValue(type, value);

            Assert.Null(actual);
        }
예제 #6
0
        public void GetValueEnumIsValid()
        {
            Type   type     = typeof(TestStatus);
            string value    = "1";
            var    expected = TestStatus.Running;

            var actual = RedisHelpers.GetValue(type, value);

            Assert.AreEqual(expected, actual);
        }
예제 #7
0
        public void GetValueGUIDIsEmpty2()
        {
            Type   type     = typeof(Guid);
            string value    = "";
            var    expected = Guid.Empty;

            var actual = RedisHelpers.GetValue(type, value);

            Assert.AreEqual(expected, actual);
        }
예제 #8
0
        public void GetValueGUIDIsValid()
        {
            Type   type     = typeof(Guid);
            string value    = "c5adb885-04a3-4b9d-861d-13e4c7669d0b";
            var    expected = Guid.Parse(value);

            var actual = RedisHelpers.GetValue(type, value);

            Assert.AreEqual(expected, actual);
        }
예제 #9
0
        public void GetValueDateTimeValid()
        {
            Type   type     = typeof(DateTime?);
            string value    = "1/1/2017 12:00:00";
            var    expected = DateTime.Parse(value);

            var actual = RedisHelpers.GetValue(type, value);

            Assert.AreEqual(expected, actual);
        }
예제 #10
0
        public void GetValueInt64Valid()
        {
            Type   type     = typeof(long?);
            string value    = "123";
            var    expected = long.Parse(value);

            var actual = RedisHelpers.GetValue(type, value);

            Assert.Equal(expected, actual);
        }
예제 #11
0
        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);
        }
예제 #12
0
        public static UserInformation ValidateSession(OracleConnection conn, string sessionId)
        {
            List <UserInformation> result = new List <UserInformation>();
            string userId = RedisHelpers.GetString("EARTH_FUSION_SESSION_" + sessionId.ToUpper());

            if (userId == null)
            {
                return(null);
            }
            string queryString = "select * from spatial_admin.earthfusion_users where USER_ID = '" + userId + "'";

            Logging.Info("EarthFusion.SessionHelpers.GetUserInformation", "Constructed query: " + queryString);
            OracleCommand command = new OracleCommand(queryString, conn);

            conn.Open();
            OracleDataReader reader = command.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    UserInformation temp = new UserInformation();
                    temp.userId             = reader.GetInt32(0);
                    temp.userName           = reader.GetString(1);
                    temp.emailAddress       = reader.GetString(2);
                    temp.userPasswordHashed = reader.GetString(3);
                    temp.accountStatus      = reader.GetString(4);
                    temp.role = reader.GetString(5);
                    result.Add(temp);
                }
            }
            finally
            {
                // always call Close when done reading.
                reader.Close();
            }
            conn.Close();
            if (result.Count < 1)
            {
                return(null);
            }
            else
            {
                return(result[0]);
            }
        }
예제 #13
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);
        }
예제 #14
0
        public RedisGetStringResult TestRedisGetString(string keyName)
        {
            RedisGetStringResult httpResponse = new RedisGetStringResult();

            httpResponse.Date    = DateTime.Now;
            httpResponse.KeyName = keyName;
            string tempResult = RedisHelpers.GetString(keyName);
            int    statusCode = (int)HttpStatusCode.OK;

            if (tempResult == null)
            {
                statusCode = (int)HttpStatusCode.NoContent;
                return(null);
            }
            httpResponse.Message = "Okay..";
            httpResponse.Value   = tempResult;
            this.HttpContext.Response.StatusCode = statusCode;
            httpResponse.StatusCode = statusCode;
            return(httpResponse);
        }
예제 #15
0
        public RedisSetStringResult TestRedisSetString(string keyName, string value)
        {
            RedisSetStringResult httpResponse = new RedisSetStringResult();

            httpResponse.Date    = DateTime.Now;
            httpResponse.KeyName = keyName;
            httpResponse.Value   = value;
            int statusCode = (int)HttpStatusCode.OK;

            if (RedisHelpers.SetString(keyName, value))
            {
                httpResponse.Message = "Set success";
            }
            else
            {
                statusCode           = (int)HttpStatusCode.InternalServerError;
                httpResponse.Message = "Something not good happened...";
            }
            this.HttpContext.Response.StatusCode = statusCode;
            httpResponse.StatusCode = statusCode;
            return(httpResponse);
        }
예제 #16
0
        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);
        }