Exemplo n.º 1
0
        public Token GenerateToken(int userId)
        {
            string   token    = Guid.NewGuid().ToString();
            DateTime issuedOn = DateTime.Now;

            //ConfigurationManager.AppSettings["AuthTokenExpiry"]
            DateTime expiredOn = DateTime.Now.AddSeconds(
                Convert.ToDouble(60 * 3));
            var tokendomain = new Token
            {
                UserId    = userId,
                AuthToken = token,
                IssuedOn  = issuedOn,
                ExpiresOn = expiredOn
            };

            var tokenRepo = new TokenRepository();

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

            return(tokenModel);
        }
Exemplo n.º 2
0
        public bool DeleteByUserId(int userId)
        {
            var tokenRepo = new TokenRepository();

            tokenRepo.Delete(tokenRepo.GetAll().Where(x => x.UserId == userId).Select(x => x.TokenId));
            tokenRepo.Save();

            var isNotDeleted = tokenRepo.GetAll().Select(x => x.UserId == userId).Any();

            return(!isNotDeleted);
        }
Exemplo n.º 3
0
        public bool Kill(string tokenId)
        {
            var tokenRepo = new TokenRepository();
            var t         = tokenRepo.GetAll().Where(x => x.AuthToken == tokenId).Select(s => s.TokenId);

            tokenRepo.Delete(t);
            tokenRepo.Save();
            var isNotDeleted = tokenRepo.GetAll().Select(x => x.AuthToken == tokenId).Any();

            if (isNotDeleted)
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 4
0
        public bool ValidateToken(string tokenId)
        {
            var tokenRepo = new TokenRepository();
            var token     = (from tokens in tokenRepo.GetAll()
                             where tokens.AuthToken == tokenId && tokens.ExpiresOn > DateTime.Now
                             select tokens).FirstOrDefault();

            if (token != null && !(DateTime.Now > token.ExpiresOn))
            {
                //ConfigurationManager.AppSettings["AuthTokenExpiry"]
                token.ExpiresOn = token.ExpiresOn.AddSeconds(
                    Convert.ToDouble(60 * 3));
                tokenRepo.Update(token);
                tokenRepo.Save();
                return(true);
            }
            return(false);
        }