public AuthenticationController(UserManager <ApplicationUser> userManager,
                                 SignInManager <ApplicationUser> signInManager,
                                 IUserRepository userRepository,
                                 IUserValidatorService userValidatorService,
                                 ITokenGeneratorService tokenGeneratorService,
                                 IOptions <TokenOptions> tokens,
                                 ILogger <AuthenticationController> logger)
     : base(userManager, logger)
 {
     _signInManager         = signInManager;
     _userRepository        = userRepository;
     _tokenOptions          = tokens.Value;
     _userValidatorService  = userValidatorService;
     _tokenGeneratorService = tokenGeneratorService;
 }
Exemplo n.º 2
0
        public async Task <Token> CreateJwtToken(ApplicationUser user, UserManager <ApplicationUser> userManager, TokenOptions tokenOptions)
        {
            var userClaims = await userManager.GetClaimsAsync(user);

            userClaims.Add(new Claim(JwtRegisteredClaimNames.Sub, user.Id));
            userClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()));

            var key         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenOptions.Key));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var expiryDate = DateTime.Now.AddYears(3);

            var jwsSecurityToken = new JwtSecurityToken(
                issuer: tokenOptions.Issuer,
                audience: tokenOptions.Issuer,
                claims: userClaims,
                expires: expiryDate,
                signingCredentials: credentials);

            return(new Token
            {
                AccessToken = new JwtSecurityTokenHandler().WriteToken(jwsSecurityToken),
                Email = user.Email,
                ExpiryDate = expiryDate
            });
        }