dev to registration #6

Merged
mfnefd merged 3 commits from dev into registration 2024-06-15 01:55:35 +04:00
4 changed files with 46 additions and 4 deletions
Showing only changes of commit d9f3faf92d - Show all commits

View File

@ -13,6 +13,7 @@
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.0" />
</ItemGroup>
<ItemGroup>

View File

@ -102,7 +102,7 @@ namespace BusinessLogic.BusinessLogic
return UserConverter.ToView(user);
}
public UserViewModel Login(string email, string password)
public string Login(string email, string password)
{
if (email is null)
{
@ -120,7 +120,7 @@ namespace BusinessLogic.BusinessLogic
{
throw new AccountException("The passwords don't match.");
}
return UserConverter.ToView(user);
return JwtProvider.Generate(user);
}
public void _validatePassword(string? password)

View File

@ -0,0 +1,41 @@
using Contracts.BindingModels;
using Contracts.ViewModels;
using Microsoft.IdentityModel.Tokens;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic.Tools
{
public class JwtProvider
{
// TODO: Переместить ключ и время в надежное место
private const string _key = "secretkey_secretkey_secretkey_secretkey";
private const int _expiresHours = 24;
public static string Generate(UserBindingModel user)
{
var signingCredentials = new SigningCredentials(
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_key)),
SecurityAlgorithms.HmacSha256);
Claim[] claims = [
new("userId", user.Id.ToString()),
new("role", user.Role.Name)
];
var token = new JwtSecurityToken(signingCredentials: signingCredentials,
expires: DateTime.UtcNow.AddHours(_expiresHours),
claims: claims);
var stringToken = new JwtSecurityTokenHandler().WriteToken(token);
return stringToken;
}
}
}

View File

@ -12,7 +12,7 @@ namespace Contracts.BusinessLogicContracts
{
public interface IUserLogic
{
UserViewModel Login(string email, string password);
string Login(string email, string password);
UserViewModel Create(UserBindingModel model);