Compare commits
No commits in common. "7511370fa394af618399eb180052ddef33a09003" and "c7e0c3823aa874866cb3c87a04df4c6a692bd618" have entirely different histories.
7511370fa3
...
c7e0c3823a
@ -7,13 +7,12 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Using Include="BCrypt.Net.BCrypt" Static="True" />
|
<Using Include="BCrypt.Net.BCrypt" Static="True"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
|
||||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.0" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
using BusinessLogic.Tools;
|
using BusinessLogic.Tools;
|
||||||
using BusinessLogic.Tools.Mail;
|
|
||||||
using BusinessLogic.Tools.Mail.MailTemplates;
|
|
||||||
using Contracts.BindingModels;
|
using Contracts.BindingModels;
|
||||||
using Contracts.BusinessLogicContracts;
|
using Contracts.BusinessLogicContracts;
|
||||||
using Contracts.Converters;
|
using Contracts.Converters;
|
||||||
@ -41,8 +39,6 @@ namespace BusinessLogic.BusinessLogic
|
|||||||
throw new Exception("Insert operation failed.");
|
throw new Exception("Insert operation failed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
MailSender.Send(new MailRegistration(user));
|
|
||||||
|
|
||||||
return UserConverter.ToView(user);
|
return UserConverter.ToView(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,7 +52,6 @@ namespace BusinessLogic.BusinessLogic
|
|||||||
{
|
{
|
||||||
throw new ElementNotFoundException();
|
throw new ElementNotFoundException();
|
||||||
}
|
}
|
||||||
MailSender.Send(new MailDeleteUser(user));
|
|
||||||
|
|
||||||
return UserConverter.ToView(user);
|
return UserConverter.ToView(user);
|
||||||
}
|
}
|
||||||
@ -104,13 +99,10 @@ namespace BusinessLogic.BusinessLogic
|
|||||||
{
|
{
|
||||||
throw new Exception("Update operation failed.");
|
throw new Exception("Update operation failed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
MailSender.Send(new MailUpdateUserData(user));
|
|
||||||
|
|
||||||
return UserConverter.ToView(user);
|
return UserConverter.ToView(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Login(string email, string password)
|
public UserViewModel Login(string email, string password)
|
||||||
{
|
{
|
||||||
if (email is null)
|
if (email is null)
|
||||||
{
|
{
|
||||||
@ -128,7 +120,7 @@ namespace BusinessLogic.BusinessLogic
|
|||||||
{
|
{
|
||||||
throw new AccountException("The passwords don't match.");
|
throw new AccountException("The passwords don't match.");
|
||||||
}
|
}
|
||||||
return JwtProvider.Generate(user);
|
return UserConverter.ToView(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void _validatePassword(string? password)
|
public void _validatePassword(string? password)
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace BusinessLogic.Tools
|
|
||||||
{
|
|
||||||
public class JwtOptions
|
|
||||||
{
|
|
||||||
public string SecretKey { get; set; } = null!;
|
|
||||||
public short ExpiresHours { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
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
|
|
||||||
{
|
|
||||||
private static string _key;
|
|
||||||
|
|
||||||
private static int _expiresHours;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetupJwtOptions(JwtOptions options)
|
|
||||||
{
|
|
||||||
_key = options.SecretKey;
|
|
||||||
_expiresHours = options.ExpiresHours;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace BusinessLogic.Tools.Mail
|
|
||||||
{
|
|
||||||
public class Mail
|
|
||||||
{
|
|
||||||
public IEnumerable<string> To { get; set; } = null!;
|
|
||||||
public string Title { get; set; } = null!;
|
|
||||||
public string Body { get; set; } = null!;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace BusinessLogic.Tools.Mail
|
|
||||||
{
|
|
||||||
public class MailOptions
|
|
||||||
{
|
|
||||||
public string Email { get; set; } = null!;
|
|
||||||
public string Password { get; set; } = null!;
|
|
||||||
public string SmtpClientHost { get; set; } = null!;
|
|
||||||
public short SmtpClientPort { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Mail;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace BusinessLogic.Tools.Mail
|
|
||||||
{
|
|
||||||
public class MailSender
|
|
||||||
{
|
|
||||||
private static string _email;
|
|
||||||
private static string _password;
|
|
||||||
private static string _smtpClientHost;
|
|
||||||
private static short _smtpClientPort;
|
|
||||||
|
|
||||||
public void SetupMailOptions(MailOptions options)
|
|
||||||
{
|
|
||||||
_email = options.Email;
|
|
||||||
_password = options.Password;
|
|
||||||
_smtpClientHost = options.SmtpClientHost;
|
|
||||||
_smtpClientPort = options.SmtpClientPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Send(Mail mail)
|
|
||||||
{
|
|
||||||
using SmtpClient client = new SmtpClient(_smtpClientHost, _smtpClientPort);
|
|
||||||
client.Credentials = new NetworkCredential(_email, _password);
|
|
||||||
client.EnableSsl = true;
|
|
||||||
|
|
||||||
using MailMessage message = new MailMessage();
|
|
||||||
|
|
||||||
message.From = new MailAddress(_email);
|
|
||||||
foreach (string to in mail.To)
|
|
||||||
{
|
|
||||||
message.To.Add(to);
|
|
||||||
}
|
|
||||||
|
|
||||||
message.Subject = mail.Title;
|
|
||||||
message.Body = mail.Body;
|
|
||||||
|
|
||||||
client.Send(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
using Contracts.BindingModels;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace BusinessLogic.Tools.Mail.MailTemplates
|
|
||||||
{
|
|
||||||
public class MailDeleteUser : Mail
|
|
||||||
{
|
|
||||||
public MailDeleteUser(UserBindingModel user)
|
|
||||||
{
|
|
||||||
To = [user.Email];
|
|
||||||
Title = "Ваш аккаунт был удален!";
|
|
||||||
Body = $"Уважаемый {user.SecondName} {user.FirstName}, Ваш аккаунт был удален навсегда насовсем.\n" +
|
|
||||||
$"Если это были не Вы... Тут уже ничего не поможет, приносим наши извинения.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
using Contracts.BindingModels;
|
|
||||||
using Contracts.ViewModels;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace BusinessLogic.Tools.Mail.MailTemplates
|
|
||||||
{
|
|
||||||
public class MailRegistration : Mail
|
|
||||||
{
|
|
||||||
public MailRegistration(UserBindingModel user)
|
|
||||||
{
|
|
||||||
To = [user.Email];
|
|
||||||
Title = "Приветствуем Вас на нашем сайте!";
|
|
||||||
Body = $"Спасибо, {user.SecondName} {user.FirstName}, что выбрали НАС.\n" +
|
|
||||||
$"Надеемся, что Вам что-то уже приглянулось!";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
using Contracts.BindingModels;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace BusinessLogic.Tools.Mail.MailTemplates
|
|
||||||
{
|
|
||||||
public class MailUpdateUserData : Mail
|
|
||||||
{
|
|
||||||
public MailUpdateUserData(UserBindingModel user)
|
|
||||||
{
|
|
||||||
To = [user.Email];
|
|
||||||
Title = "Данные пользователя были обновлены!";
|
|
||||||
Body = $"Уважаемый {user.SecondName} {user.FirstName}, Ваши данные были обвновлены.\n" +
|
|
||||||
$"Если это были не Вы, то что поделать, Вам придется менять пароль.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -12,7 +12,7 @@ namespace Contracts.BusinessLogicContracts
|
|||||||
{
|
{
|
||||||
public interface IUserLogic
|
public interface IUserLogic
|
||||||
{
|
{
|
||||||
string Login(string email, string password);
|
UserViewModel Login(string email, string password);
|
||||||
|
|
||||||
UserViewModel Create(UserBindingModel model);
|
UserViewModel Create(UserBindingModel model);
|
||||||
|
|
||||||
|
@ -56,11 +56,13 @@ namespace RestAPI.Controllers
|
|||||||
catch (AccountException ex)
|
catch (AccountException ex)
|
||||||
{
|
{
|
||||||
_logger.LogWarning(ex, "Wrong registration data");
|
_logger.LogWarning(ex, "Wrong registration data");
|
||||||
|
throw;
|
||||||
return Results.BadRequest(ex.Message);
|
return Results.BadRequest(ex.Message);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error create user");
|
_logger.LogError(ex, "Error create user");
|
||||||
|
throw;
|
||||||
return Results.Problem(ex.Message);
|
return Results.Problem(ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
using BusinessLogic.BusinessLogic;
|
using BusinessLogic.BusinessLogic;
|
||||||
using BusinessLogic.Tools;
|
|
||||||
using BusinessLogic.Tools.Mail;
|
|
||||||
using Contracts.BusinessLogicContracts;
|
using Contracts.BusinessLogicContracts;
|
||||||
using Contracts.StorageContracts;
|
using Contracts.StorageContracts;
|
||||||
using DatabaseImplement.Implements;
|
using DatabaseImplement.Implements;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Mail;
|
|
||||||
|
|
||||||
const string VERSION = "v1";
|
const string VERSION = "v1";
|
||||||
const string TITLE = "21GunsRestAPI";
|
const string TITLE = "21GunsRestAPI";
|
||||||
@ -24,9 +21,6 @@ builder.Services.AddTransient<IUserLogic, UserLogic>();
|
|||||||
builder.Services.AddTransient<IRoleStorage, RoleStorage>();
|
builder.Services.AddTransient<IRoleStorage, RoleStorage>();
|
||||||
builder.Services.AddTransient<IUserStorage, UserStorage>();
|
builder.Services.AddTransient<IUserStorage, UserStorage>();
|
||||||
|
|
||||||
builder.Services.AddSingleton<JwtProvider>();
|
|
||||||
builder.Services.AddSingleton<MailSender>();
|
|
||||||
|
|
||||||
#endregion DI
|
#endregion DI
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
@ -38,26 +32,6 @@ builder.Services.AddSwaggerGen(c =>
|
|||||||
});
|
});
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
var jwtProvider = app.Services.GetService<JwtProvider>();
|
|
||||||
var mailSender = app.Services.GetService<MailSender>();
|
|
||||||
|
|
||||||
#region Setup config
|
|
||||||
|
|
||||||
string? getSection(string section) => builder.Configuration?.GetSection(section)?.Value?.ToString();
|
|
||||||
jwtProvider?.SetupJwtOptions(new()
|
|
||||||
{
|
|
||||||
SecretKey = getSection("JwtOptions:SecretKey") ?? string.Empty,
|
|
||||||
ExpiresHours = Convert.ToInt16(getSection("JwtOptions:ExpiresHours"))
|
|
||||||
});
|
|
||||||
mailSender?.SetupMailOptions(new()
|
|
||||||
{
|
|
||||||
Email = getSection("MailOptions:Email") ?? string.Empty,
|
|
||||||
Password = getSection("MailOptions:Password") ?? string.Empty,
|
|
||||||
SmtpClientHost = getSection("MailOptions:SmtpClientHost") ?? string.Empty,
|
|
||||||
SmtpClientPort = Convert.ToInt16(getSection("MailOptions:SmtpClientPort"))
|
|
||||||
});
|
|
||||||
|
|
||||||
#endregion Setup config
|
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
|
@ -5,15 +5,5 @@
|
|||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"JwtOptions": {
|
|
||||||
"SecretKey": "secretkey_secretkey_secretkey_secretkey",
|
|
||||||
"ExpiresHours": 24
|
|
||||||
},
|
|
||||||
"MailOptions": {
|
|
||||||
"Email": "21.guns.site@gmail.com",
|
|
||||||
"Password": "tiss lpgf nhap qnfc",
|
|
||||||
"SmtpClientHost": "smtp.gmail.com",
|
|
||||||
"SmtpClientPort": "587"
|
|
||||||
},
|
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*"
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user