registration back #7
@ -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>
|
||||
|
@ -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)
|
||||
|
41
BusinessLogic/Tools/JwtProvider.cs
Normal file
41
BusinessLogic/Tools/JwtProvider.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user