using Services.AuthAPI.Models;
namespace Services.AuthAPI.Service.IService
{
public interface IJwtTokenGenerator
{
string GenerateToken(ApplicationUser applicationUser);
}
}
builder.Services.AddControllers();
builder.Services.AddScoped<IJwtTokenGenerator, JwtTokenGenerator>();
using Services.AuthAPI.Models;
using Services.AuthAPI.Service.IService;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Data;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
namespace Services.AuthAPI.Service
{
public class JwtTokenGenerator : IJwtTokenGenerator
{
private readonly JwtOptions _jwtOptions;
public JwtTokenGenerator(JwtOptions jwtOptions)
{
_jwtOptions = jwtOptions;
}
public string GenerateToken(ApplicationUser applicationUser)
{
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_jwtOptions.Secret);
var claimList = new List<Claim>
{
new Claim(JwtRegisteredClaimNames.Email, applicationUser.Email),
new Claim(JwtRegisteredClaimNames.Sub, applicationUser.Id),
new Claim(JwtRegisteredClaimNames.Name, applicationUser.UserName)
};
var tokenDescriptor = new SecurityTokenDescriptor
{
Audience = _jwtOptions.Audience,
Issuer = _jwtOptions.Issuer,
Subject = new ClaimsIdentity(claimList),
Expires = DateTime.UtcNow.AddDays(7),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
}
}