Ejemplo n.º 1
0
        public async Task deleteAllByIDAsync(int BTID)
        {
            try
            {
                var answers = await _context.BrainTeaserWinners.Where(x => x.BrainTeaserID == BTID).ToListAsync();

                _context.BrainTeaserWinners.RemoveRange(answers);
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 2
0
        private async Task <string> GenerateRefreshToken(Guid id)
        {
            try
            {
                var    randomNumber = new byte[32];
                string token;
                using (var rng = RandomNumberGenerator.Create())
                {
                    rng.GetBytes(randomNumber);
                    token = Convert.ToBase64String(randomNumber);
                }

                await _context.RefreshTokens.AddAsync(new RefreshToken
                {
                    UserId      = id,
                    Token       = token,
                    ExpiresDate = DateTime.Now.AddDays(Convert.ToDouble(30))
                });

                await _context.SaveChangesAsync();

                return(token);
            }
            catch (Exception)
            {
                return(null);
            }
        }
        public async Task <Response <Token> > RefreshToken(string token, string refreshToken)
        {
            try
            {
                var principal = GetPrincipalFromExpiredToken(token);
                if (principal == null)
                {
                    return(new Response <Token>(400, "Invalid access token"));
                }
                var email = principal.Identity.Name;
                var user  = await _userManager.FindByEmailAsync(email);

                if (user == null)
                {
                    return(new Response <Token>(404, "User not found"));
                }
                var dbToken = _context.RefreshTokens
                              .FirstOrDefault(rt => rt.UserId == user.Id && rt.Token == refreshToken);
                if (dbToken == null)
                {
                    return(new Response <Token>(400, "Invalid refresh token"));
                }
                if (dbToken.ExpiresDate < DateTime.Now)
                {
                    _context.RefreshTokens.Remove(dbToken);
                    await _context.SaveChangesAsync();

                    return(new Response <Token>(400, "Expired refresh token"));
                }
                var data = await _jwt.GenerateJwt(user);

                if (data.Succeeded())
                {
                    _context.RefreshTokens.Remove(dbToken);
                    await _context.SaveChangesAsync();
                }
                return(data);
            }
            catch
            {
                return(new Response <Token>(520, "Unknown error"));
            }
        }
Ejemplo n.º 4
0
 public async Task <bool> SaveAsync()
 {
     return(await _tcontext.SaveChangesAsync() >= 0);
 }