Kétfaktoros első része ok
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="hu">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Megerősítő kód</title>
|
||||
</head>
|
||||
<body style="font-family: Arial, sans-serif; background-color: #f9f9f9; padding: 20px;">
|
||||
|
||||
<p style="font-size: 18px;">
|
||||
<strong>Workflow Check Application Web</strong> megerősítő kódja:
|
||||
</p>
|
||||
|
||||
<p style="font-size: 28px; font-weight: bold; color: #333; margin-top: 10px;">
|
||||
#token2F#
|
||||
</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -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);
|
||||
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WorkFlowCheck.BL.Static
|
||||
{
|
||||
public static class CodeGenerator2F
|
||||
{
|
||||
private static readonly Random _random = new Random();
|
||||
private const string SecretKey = "874512WcfApplicationNuvolar.hu!%mBX210jUUKL";
|
||||
private static readonly byte[] KeyBytes = Encoding.UTF8.GetBytes(SecretKey);
|
||||
|
||||
public static string GenerateSixDigitCode()
|
||||
{
|
||||
return _random.Next(100000, 1000000).ToString(); // 100000 - 999999
|
||||
}
|
||||
public static string GenerateToken2F(string value1, string value2, string value3)
|
||||
{
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim("val1", value1),
|
||||
new Claim("val2", value2),
|
||||
new Claim("val3", value3)
|
||||
};
|
||||
|
||||
var descriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(claims),
|
||||
Expires = DateTime.UtcNow.AddSeconds(60),
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(KeyBytes), SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
|
||||
var token = tokenHandler.CreateToken(descriptor);
|
||||
return tokenHandler.WriteToken(token);
|
||||
}
|
||||
public static bool ValidateToken2F(string token, string expected1, string expected2, string expected3)
|
||||
{
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
|
||||
try
|
||||
{
|
||||
var parameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false,
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(KeyBytes),
|
||||
ClockSkew = TimeSpan.Zero // nincs türelmi idő
|
||||
};
|
||||
|
||||
var principal = tokenHandler.ValidateToken(token, parameters, out var validatedToken);
|
||||
|
||||
var val1 = principal.FindFirst("val1")?.Value;
|
||||
var val2 = principal.FindFirst("val2")?.Value;
|
||||
var val3 = principal.FindFirst("val3")?.Value;
|
||||
|
||||
return val1 == expected1 && val2 == expected2 && val3 == expected3;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,14 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="HtmlTemplates\Token2FBody.html" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="HtmlTemplates\Token2FBody.html" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="DocumentFormat.OpenXml" Version="3.3.0" />
|
||||
|
||||
Reference in New Issue
Block a user