Пример #1
0
        /// <summary>
        ///  Function to generate unique token with expiry against the provided userId.
        ///  Also add a record in database for generated token.
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Token GenerateToken(int userId)
        {
            string token = Guid.NewGuid().ToString();
            DateTime issuedOn = DateTime.Now;
            DateTime expiredOn = DateTime.Now.AddSeconds(Convert.ToDouble(ConfigurationManager.AppSettings["AuthTokenExpiry"]));
            var tokendomain = new Token
                                  {
                                      UserId = userId,
                                      AuthToken = token,
                                      IssuedOn = issuedOn,
                                      ExpiresOn = expiredOn
                                  };

            _tokenMethod.InsertToken(tokendomain);
            var tokenModel = new Token()
                                 {
                                     UserId = userId,
                                     IssuedOn = issuedOn,
                                     ExpiresOn = expiredOn,
                                     AuthToken = token
                                 };

            return tokenModel;
        }
Пример #2
0
        /// <summary>
        /// Used to update the token information
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool UpdateToken(Token token)
        {
            try
            {
                using (var tde = new TimeDifferenceEntities())
                {
                    var tokenInfo = tde.Tokens.FirstOrDefault(m => m.AuthToken == token.AuthToken);
                    if (tokenInfo == null)
                        return false;

                    tokenInfo.AuthToken = token.AuthToken;
                    tokenInfo.ExpiresOn = token.ExpiresOn;
                    tokenInfo.IssuedOn = token.IssuedOn;
                    tokenInfo.UserId = token.UserId;
                    tde.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
Пример #3
0
 /// <summary>
 /// Used to add token in the database
 /// </summary>
 /// <returns></returns>
 public bool InsertToken(Token token)
 {
     try
     {
         using (var tde = new TimeDifferenceEntities())
         {
             tde.Tokens.Add(new Token
             {
                 AuthToken = token.AuthToken,
                 ExpiresOn = token.ExpiresOn,
                 IssuedOn = token.IssuedOn,
                 UserId = token.UserId,
             });
             tde.SaveChanges();
             return true;
         }
     }
     catch (Exception ex)
     {
         return false;
     }
 }