예제 #1
0
        private async Task <JwtSecurityToken> GenerateTokenCore(UserIdentityData user, DateTime expires)
        {
            var claims = await this.claimsFactory.GetIdentityClaims(user);

            AddDefaultJwtClaims(user, claims);
            return(this.CreateJwt(claims, expires));
        }
예제 #2
0
 private static void AddDefaultJwtClaims(UserIdentityData user, IList <Claim> claims)
 {
     claims.Add(new Claim(JwtRegisteredClaimNames.Sub, user.Email));
     claims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()));
     claims.Add(new Claim(
                    JwtRegisteredClaimNames.Iat,
                    DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture),
                    ClaimValueTypes.Integer64));
 }
예제 #3
0
        /// <summary>
        /// Generates a JWT for the user
        /// </summary>
        /// <param name="user">the user</param>
        /// <returns>JWT for the user</returns>
        public async Task <JwtSecurityToken> GenerateToken(UserIdentityData user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            var expires = DateTime.UtcNow.AddMinutes(60);

            return(await this.GenerateTokenCore(user, expires));
        }
예제 #4
0
        private async Task <AuthenticationResultDto> GetAuthenticationResult(UserIdentityData userIdentity)
        {
            if (!(await this.signInManager.CanSignInAsync(userIdentity)))
            {
                return(null);
            }

            var jwtToken = await this.tokenGenerator.GenerateToken(userIdentity);

            var(token, expires, issuedAt) = (
                new JwtSecurityTokenHandler().WriteToken(jwtToken),
                jwtToken.ValidTo,
                jwtToken.IssuedAt
                );

            return(new AuthenticationResultDto()
            {
                User = this.mapper.Map <UserIdentityInfoDto>(userIdentity),
                Token = token,
                TokenExpirationDate = expires,
                TokenIssuedAtDate = issuedAt,
            });
        }