Beispiel #1
0
        /// <summary>
        /// Method to validate token against expiry and existence in database.
        /// </summary>
        /// <param name="tokenId"></param>
        /// <returns></returns>
        public bool ValidateToken(string tokenValue, string userId)
        {
            Token token;
            var   db = new DBAMPContext();
            int   Id = Convert.ToInt32(userId);

            token = db.Tokens.Where(t => t.AuthToken == tokenValue && t.UserId == Id).FirstOrDefault <Token>();

            if (token != null)
            {
                if (!(DateTime.Now > token.ExpiresOn))  //if validated token, extend Expiry Time
                {
                    token.ExpiresOn = token.ExpiresOn.AddSeconds(
                        Convert.ToDouble(ConfigurationManager.AppSettings["AuthTokenExpiry"]));

                    int rtn = db.ApiTokenUpdate(token.UserId, token.AuthToken, token.ExpiresOn);

                    return(true);
                }
                else  //If expired token, delete it from DB
                {
                    //int rtn = db.ApiTokenDelete(token.UserId, token.AuthToken);
                    KillToken(token);
                    return(false);
                }
            }
            return(false);
        }