Пример #1
0
        private async Task <RefreshToken> CreateRefreshTokenAsync(string userId)
        {
            var refreshToken = _tokenRepo.CreateRefreshToken(userId);
            await _uow.CompleteAsync();

            return(refreshToken);
        }
Пример #2
0
        /// <summary>
        /// Permet de créer un RefreshToken
        /// </summary>
        /// <param name="token"></param>
        /// <returns>Boolean</returns>
        public async Task <bool> CreateRefreshToken(RefreshToken token)
        {
            var result = false;

            if (token != null)
            {
                if (!String.IsNullOrWhiteSpace(token.TokenValue) && !String.IsNullOrWhiteSpace(token.UserId))
                {
                    await _repo.CreateRefreshToken(token);

                    result = true;
                }
            }

            return(result);
        }
Пример #3
0
        private async Task <AuthenticationResponse> GenerateAuthenticationResultAsync(User user)
        {
            var tokenHandler = new JwtSecurityTokenHandler();
            var key          = Encoding.ASCII.GetBytes(_jwtSettings.Secret);

            var claims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.Sub, user.Email),
                new Claim(JwtRegisteredClaimNames.Email, user.Email),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim("id", user.Id)
            };

            var userClaims = await _userManager.GetClaimsAsync(user);

            claims.AddRange(userClaims);

            var userRoles = await _userManager.GetRolesAsync(user);

            foreach (var userRole in userRoles)
            {
                claims.Add(new Claim(ClaimTypes.Role, userRole));
                var role = await _roleManager.FindByNameAsync(userRole);

                if (role == null)
                {
                    continue;
                }
                var roleClaims = await _roleManager.GetClaimsAsync(role);

                foreach (var roleClaim in roleClaims)
                {
                    if (!claims.Contains(roleClaim))
                    {
                        claims.Add(roleClaim);
                    }
                }
            }

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.UtcNow.Add(_jwtSettings.TokenLifeTime),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };

            var token = tokenHandler.CreateToken(tokenDescriptor);

            var refreshToken = new RefreshToken
            {
                JwtId        = token.Id,
                UserId       = user.Id,
                CreationDate = DateTime.UtcNow,
                ExpiryDate   = DateTime.UtcNow.AddMonths(2)
            };

            var saved = await _refreshTokenRepository.CreateRefreshToken(refreshToken);

            if (!saved)
            {
                return(new AuthenticationResponse
                {
                    Success = false,
                    Token = null,
                    RefreshToken = null,
                    ErrorMessages = new[] { "Refresh token cannot be created correctly." }
                });
            }

            return(new AuthenticationResponse
            {
                Success = true,
                Token = tokenHandler.WriteToken(token),
                RefreshToken = refreshToken.Token
            });
        }
Пример #4
0
 public void CreateRefreshToken(RefreshTokenModel model)
 {
     _refreshTokenRepository.CreateRefreshToken(model);
 }