Example #1
0
 public AuthenticationController(UserManager <ApplicationUser> userManager,
                                 SignInManager <ApplicationUser> signInManager,
                                 IUserRepository userRepository,
                                 IPushNotificationTokensRepository pushNotificationTokensRepository,
                                 IOptions <TokenOptions> tokens,
                                 ILogger <AuthenticationController> logger)
     : base(userManager, logger)
 {
     _signInManager  = signInManager;
     _userRepository = userRepository;
     _tokenOptions   = tokens.Value;
     _pushNotificationTokensRepository = pushNotificationTokensRepository;
 }
Example #2
0
        public static async Task <Token> Create(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),
                FullName = user.FullName,
                Email = user.Email,
                PhoneNumber = user.PhoneNumber,
                UserId = user.Id,
                ExpiryDate = expiryDate,
                DateUserCreated = user.DateCreated
            });
        }