JwtToken 1.0
This commit is contained in:
@@ -14,7 +14,8 @@ namespace WorkFlowCheck.BL.Mappings
|
||||
{
|
||||
public MapperProfile()
|
||||
{
|
||||
CreateMap<User, UserDTO>();
|
||||
CreateMap<User, UserDTO>().ForMember(dest => dest.RoleDTO, opt => opt.MapFrom(src => src.UserRoles.Select(ur => ur.Role)));
|
||||
CreateMap<Role, RoleDTO>();
|
||||
CreateMap<UserDTO, User>()
|
||||
.ForMember(dest => dest.PasswordHash, opt => opt.MapFrom<PasswordHashResolver>());
|
||||
|
||||
|
||||
@@ -11,5 +11,6 @@ namespace WorkFlowCheck.BL.Services.Interfaces
|
||||
{
|
||||
Task<UserDTO> GetUser(int Id);
|
||||
Task<UserDTO> Authenticate(string UserName, string Password);
|
||||
string GenerateJwtToken(UserDTO userDTO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,11 @@ using WorkFlowCheck.Common.DTO;
|
||||
using WorkFlowCheck.BL.Services.Interfaces;
|
||||
using WorkFlowCheck.DL;
|
||||
using WorkFlowCheck.Common.Security;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using WorkFlowCheck.DL.Entities;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace WorkFlowCheck.BL.Services
|
||||
{
|
||||
@@ -17,14 +22,17 @@ namespace WorkFlowCheck.BL.Services
|
||||
{
|
||||
private AppDbContext _dbContext;
|
||||
private IMapper _mapper;
|
||||
public UserService(AppDbContext dbContext, IMapper mapper)
|
||||
private IConfiguration _configuration;
|
||||
|
||||
public UserService(AppDbContext dbContext, IMapper mapper, IConfiguration configuration)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_mapper = mapper;
|
||||
_configuration = configuration;
|
||||
}
|
||||
public async Task<UserDTO> GetUser(int Id)
|
||||
{
|
||||
var retVal = new UserDTO();
|
||||
var retVal = new UserDTO() { RoleDTO = new List<RoleDTO>() };
|
||||
|
||||
try
|
||||
{
|
||||
@@ -41,15 +49,37 @@ namespace WorkFlowCheck.BL.Services
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public async Task<UserDTO> Authenticate(string userName, string password)
|
||||
public async Task<UserDTO> Authenticate(string userName, string password)
|
||||
{
|
||||
var retVal = new UserDTO();
|
||||
var retVal = new UserDTO() { RoleDTO = new List<RoleDTO>() };
|
||||
|
||||
try
|
||||
{
|
||||
var user = await _dbContext.Users.Where(w => w.UserName == userName).FirstOrDefaultAsync();
|
||||
var user = await _dbContext.Users
|
||||
.Include(i => i.UserRoles)
|
||||
.ThenInclude(i => i.Role)
|
||||
.Where(w => w.UserName == userName)
|
||||
.FirstOrDefaultAsync();
|
||||
if (user != null && PasswordHasher.VerifyPassword(user.PasswordHash, password))
|
||||
{
|
||||
|
||||
var userDTO = new UserDTO()
|
||||
{
|
||||
Id = user.Id,
|
||||
UserName = user.UserName,
|
||||
RoleDTO = new List<RoleDTO>(),
|
||||
};
|
||||
foreach (var item in user.UserRoles)
|
||||
{
|
||||
userDTO.RoleDTO.Add(new RoleDTO()
|
||||
{
|
||||
Id = item.Role.Id,
|
||||
RoleName = item.Role.RoleName,
|
||||
});
|
||||
}
|
||||
user.JwtToken = GenerateJwtToken(userDTO);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
retVal = _mapper.Map<UserDTO>(user);
|
||||
}
|
||||
}
|
||||
@@ -59,5 +89,36 @@ namespace WorkFlowCheck.BL.Services
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public string GenerateJwtToken(UserDTO userDTO)
|
||||
{
|
||||
var jwtSettings = _configuration.GetSection("Jwt");
|
||||
var secretKey = jwtSettings["SecretKey"];
|
||||
var issuer = jwtSettings["Issuer"];
|
||||
var audience = jwtSettings["Audience"];
|
||||
var tokenLifetime = int.Parse(jwtSettings["TokenLifetimeMinutes"]);
|
||||
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim(ClaimTypes.Name, userDTO.UserName),
|
||||
new Claim(ClaimTypes.NameIdentifier, userDTO.Id.ToString())
|
||||
};
|
||||
|
||||
foreach (var role in userDTO.RoleDTO)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, role.RoleName));
|
||||
}
|
||||
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
|
||||
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||
var token = new JwtSecurityToken(
|
||||
issuer: issuer,
|
||||
audience: audience,
|
||||
claims: claims,
|
||||
expires: DateTime.UtcNow.AddMinutes(tokenLifetime),
|
||||
signingCredentials: creds);
|
||||
|
||||
return new JwtSecurityTokenHandler().WriteToken(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user