Esempio n. 1
0
        public async Task <BearerAuthResponse> RefreshTokenAsync(Guid?userId, string refreshToken)
        {
            try
            {
                User user = null;
                if (userId.HasValue)
                {
                    user = await this.userManager.FindByIdAsync(userId.ToString());
                }
                else
                {
                    user = (await this.uow.GetStandardRepository().QueryAsync <User>(x => x.RefreshToken == refreshToken)).FirstOrDefault();
                }

                if (user != null && user.RefreshToken == refreshToken && user.RefreshTokenExpiration.HasValue && user.RefreshTokenExpiration > DateTime.Now)
                {
                    var userClaims = await GetAllUserClaimsAsync(user);

                    string jwt = BuildJwtToken(user, userClaims);

                    return(BearerAuthResponse.SuccessResult(jwt, refreshToken));
                }

                return(BearerAuthResponse.FailedResult);
            }
            catch (Exception)
            {
                return(BearerAuthResponse.FailedResult);
            }
        }
Esempio n. 2
0
        public async Task <BearerAuthResponse> BuildTokenAsync(string email, string password)
        {
            try
            {
                var user = await this.userManager.FindByEmailAsync(email);

                if (user.EmailConfirmed && !(await this.userManager.IsLockedOutAsync(user)) && await this.userManager.CheckPasswordAsync(user, password))
                {
                    var userClaims = await GetAllUserClaimsAsync(user);

                    string jwt          = BuildJwtToken(user, userClaims);
                    string refreshToken = $"{Guid.NewGuid().ToString().Replace("-", "0")}{Guid.NewGuid().ToString().Replace("-", "1")}";
                    user.RefreshToken           = refreshToken;
                    user.RefreshTokenExpiration = DateTime.Now.AddMonths(1);
                    this.uow.GetStandardRepository().Update <User>(user);
                    await this.uow.SaveChangesAsync();

                    return(BearerAuthResponse.SuccessResult(jwt, refreshToken));
                }

                return(BearerAuthResponse.FailedResult);
            }
            catch (Exception)
            {
                return(BearerAuthResponse.FailedResult);
            }
        }