...
This commit is contained in:
@@ -35,7 +35,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Service">
|
||||
<HintPath>..\..\..\LocalGit\TaxPayerTools\Service\bin\Debug\Service.dll</HintPath>
|
||||
<HintPath>..\..\Dlls\Service.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
|
@@ -7,6 +7,7 @@ using Shared.DTOs;
|
||||
namespace Back.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
@@ -19,12 +20,17 @@ namespace Back.Controllers
|
||||
}
|
||||
[HttpPost("authenticate")]
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<UserAuthenticationDTO>> Login(Authentication model)
|
||||
public async Task<ActionResult<UserAuthenticationDTO>> Login([FromBody]Authentication model)
|
||||
{
|
||||
if (!ModelState.IsValid) return BadRequest(model);
|
||||
var result = await _servUser.UserAuthentication(model.Username, model.Password);
|
||||
if (result != null) return Ok(result);
|
||||
else return NotFound("کاربری با این مشخصات یافت نشد");
|
||||
}
|
||||
[HttpGet("test")]
|
||||
|
||||
public async Task<ActionResult> test()
|
||||
{
|
||||
return Ok();
|
||||
|
||||
|
||||
|
||||
|
@@ -1,8 +1,11 @@
|
||||
using Back;
|
||||
using Back.Data.Contracts;
|
||||
using Back.Data.Infrastructure.Repository;
|
||||
using Back.Services;
|
||||
using Back.Validations;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
using TaxPayer.Infrastructure.Persistence;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -46,6 +49,24 @@ builder.Services.AddCors(options =>
|
||||
});
|
||||
});
|
||||
|
||||
#region JWT
|
||||
builder.Services.AddAuthentication("Bearer")
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new()
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = Fixedvalues.Issuer,
|
||||
ValidAudience = Fixedvalues.Audience,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(
|
||||
Encoding.ASCII.GetBytes(Fixedvalues.SecretForKey))
|
||||
};
|
||||
}
|
||||
);
|
||||
#endregion
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
@@ -57,6 +78,7 @@ if (app.Environment.IsDevelopment())
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseCors(origins);
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
@@ -13,18 +13,16 @@ namespace Back.Services
|
||||
public class servUser
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly CheckPermission _checkPermission;
|
||||
private readonly servPermission _servPermission;
|
||||
|
||||
private readonly servNotification _servNotification;
|
||||
private readonly IAsyncRepository<User> _RepoUser;
|
||||
private readonly IAsyncRepository<PermissionPeriod> _RepoPermissionPeriod;
|
||||
public servUser(IConfiguration configuration,
|
||||
CheckPermission checkPermission, servPermission servPermission
|
||||
, servNotification servNotification, IAsyncRepository<User> RepoUser, IAsyncRepository<PermissionPeriod> RepoPermissionPeriod)
|
||||
public servUser(IConfiguration configuration
|
||||
, servNotification servNotification
|
||||
, IAsyncRepository<User> RepoUser
|
||||
, IAsyncRepository<PermissionPeriod> RepoPermissionPeriod)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_checkPermission = checkPermission;
|
||||
_servPermission = servPermission;
|
||||
_servNotification = servNotification;
|
||||
_RepoUser = RepoUser;
|
||||
_RepoPermissionPeriod = RepoPermissionPeriod;
|
||||
@@ -32,16 +30,16 @@ namespace Back.Services
|
||||
public async Task<User?> GetUserByUserNameAndPassword(string UserName, string Password)
|
||||
{
|
||||
return await _RepoUser.Get(w => w.Username == UserName && w.Password == Password.encrypted() && w.IsActive)
|
||||
.Include(i => i.RolUsers)
|
||||
.ThenInclude(ti => ti.rolePermissions)
|
||||
.Include(i => i.RolUsers)
|
||||
.ThenInclude(ti=>ti.Company)
|
||||
.ThenInclude(ti => ti.PermissionPeriods)
|
||||
.ThenInclude(ti => ti.Permission)
|
||||
// .Include(i => i.RolUsers)
|
||||
// .ThenInclude(ti => ti.rolePermissions)
|
||||
// .Include(i => i.RolUsers)
|
||||
// .ThenInclude(ti=>ti.Company)
|
||||
//.ThenInclude(ti => ti.PermissionPeriods)
|
||||
// .ThenInclude(ti => ti.Permission)
|
||||
.Include(ti=>ti.RolUsers)
|
||||
.ThenInclude(ti => ti.Company)
|
||||
.ThenInclude(ti => ti.PermissionPeriods)
|
||||
.ThenInclude(ti => ti.CalculationType)
|
||||
//.ThenInclude(ti => ti.PermissionPeriods)
|
||||
// .ThenInclude(ti => ti.CalculationType)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
public async Task<UserAuthenticationDTO?> UserAuthentication(string UserNameORUserID, string Password="")
|
||||
@@ -158,7 +156,7 @@ namespace Back.Services
|
||||
{
|
||||
return await _RepoUser.Get(w => w.ID == UserID).FirstOrDefaultAsync();
|
||||
}
|
||||
public async void SetTokenAndDateLogininDB(int UserID,string Token)
|
||||
public async Task SetTokenAndDateLogininDB(int UserID,string Token)
|
||||
{
|
||||
var user = await GetUserByUserID(UserID);
|
||||
if (user != null)
|
||||
@@ -274,7 +272,7 @@ namespace Back.Services
|
||||
|
||||
string Token = new JwtSecurityTokenHandler()
|
||||
.WriteToken(jwtSecurityToke);
|
||||
SetTokenAndDateLogininDB(UserId, Token);
|
||||
await SetTokenAndDateLogininDB(UserId, Token);
|
||||
//_contextMongodb.InsertItem(new SysLog()
|
||||
//{
|
||||
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
|
||||
|
Reference in New Issue
Block a user