Kétfaktoros első része ok

This commit is contained in:
2025-04-10 20:49:01 +02:00
parent 623a3c49b1
commit d5a7895efa
9 changed files with 284 additions and 15 deletions
@@ -14,6 +14,8 @@ namespace WorkFlowCheck.BL.Services.Interfaces
Task<bool> DeleteUserAsync(int Id);
Task<UserDTO> UpdateUserAsync(UserDTO userDTO);
Task<UserDTO> Authenticate(string UserName, string Password);
Task<string> Authenticate2F1(string UserName, string Password);
Task<UserDTO> Authenticate2F2(string UserName, string Password, string token2FA);
Task<UserDTO> AuthenticateNFC(string NFCCode);
+62 -2
View File
@@ -16,17 +16,24 @@ using System.Security.Claims;
using WorkFlowCheck.DL.Entities;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Identity;
using WorkFlowCheck.BL.Static;
using System.Reflection;
namespace WorkFlowCheck.BL.Services
{
public class UserService : BaseService, IUserService
{
private IConfiguration _configuration;
private IMessageService _messageService;
public UserService(AppDbContext dbContext, IMapper mapper, IConfiguration configuration) : base(dbContext, mapper)
public UserService(AppDbContext dbContext,
IMapper mapper,
IConfiguration configuration,
IMessageService messageService) : base(dbContext, mapper)
{
_configuration = configuration;
_messageService = messageService;
}
public async Task<UserDTO> GetUserAsync(int Id)
@@ -90,7 +97,6 @@ namespace WorkFlowCheck.BL.Services
.FirstOrDefaultAsync();
if (user != null && PasswordHasher.VerifyPassword(user.PasswordHash, password))
{
var userDTO = new UserDTO()
{
Id = user.Id,
@@ -117,6 +123,46 @@ namespace WorkFlowCheck.BL.Services
}
return retVal;
}
public async Task<string> Authenticate2F1(string userName, string password)
{
var retVal = "";
try
{
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))
{
if (!string.IsNullOrEmpty(user.Email))
{
var token2F = CodeGenerator2F.GenerateSixDigitCode();
await _messageService.SendMailAsync(new List<string>()
{
user.Email,
}, "Bejelentkezés megerősítése",
Get2FConfirmationMessageBody(token2F));
retVal = CodeGenerator2F.GenerateToken2F(userName, password, token2F);
return retVal;
}
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
public async Task<UserDTO> Authenticate2F2(string UserName, string Password, string token2FA)
{
var retVal = new UserDTO() { RoleDTO = new List<RoleDTO>() };
return retVal;
}
public async Task<UserDTO> AuthenticateNFC(string NFCCode)
{
var retVal = new UserDTO() { RoleDTO = new List<RoleDTO>() };
@@ -241,6 +287,20 @@ namespace WorkFlowCheck.BL.Services
return retVal;
}
private string Get2FConfirmationMessageBody(string token2F)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "WorkFlowCheck.BL.HtmlTemplates.Token2FBody.html";
using var stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null)
throw new FileNotFoundException($"Nem található a beágyazott erőforrás: {resourceName}");
using var reader = new StreamReader(stream);
var html = reader.ReadToEnd();
return html.Replace("#token2F#", token2F);
}
public async Task<RoleDTO> GetRoleAsync(int Id)
{
var retVal = new RoleDTO();