示例#1
0
        private static Task <ClaimsIdentity> CreateClaims(User user)
        {
            var claims = new ClaimsIdentity();

            claims.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.UserId.ToString()));
            claims.AddClaim(new Claim(ClaimTypes.Name, user.Name));
            claims.AddClaim(new Claim("username", user.Username));

            return(Task.FromResult(claims));
        }
示例#2
0
        private async Task <AuthResponse> GenerateJwt(User user)
        {
            var claims = await CreateClaims(user);

            var handler     = new JwtSecurityTokenHandler();
            var issuer      = _applicationConfiguration.Issuer();
            var secret      = _applicationConfiguration.Secret();
            var credentials = new SigningCredentials(new SymmetricSecurityKey(secret), SecurityAlgorithms.HmacSha512);
            var token       = handler.CreateJwtSecurityToken(issuer, null, claims, DateTime.UtcNow,
                                                             DateTime.UtcNow.AddDays(7), DateTime.UtcNow, credentials);

            user.Password = null;
            return(new AuthResponse
            {
                User = user,
                Jwt = handler.WriteToken(token),
            });
        }