...
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,
|
||||
|
@@ -2,6 +2,7 @@
|
||||
@using Shared.DTOs
|
||||
@inject HttpClient _hc
|
||||
@inject NavigationManager nav
|
||||
@inject UserAuthenticationDTO userinfo
|
||||
<PageTitle>ثبت نام</PageTitle>
|
||||
<main>
|
||||
|
||||
@@ -88,6 +89,9 @@
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
if (userinfo!=null)
|
||||
nav.NavigateTo("/");
|
||||
|
||||
editContext = new EditContext(model);
|
||||
messageStore = new(editContext);
|
||||
await base.OnInitializedAsync();
|
||||
|
@@ -1,6 +1,9 @@
|
||||
@page "/Sign-in"
|
||||
@using Shared.DTOs
|
||||
|
||||
@inject HttpClient _hc
|
||||
@inject NavigationManager nav
|
||||
@inject UserAuthenticationDTO userinfo
|
||||
<PageTitle>ورود</PageTitle>
|
||||
<main>
|
||||
|
||||
@@ -107,6 +110,15 @@
|
||||
<button class="signin-btn ">ورود</button>
|
||||
</div>
|
||||
</EditForm>
|
||||
<div class="row">
|
||||
<Alert hidden="@Hidealert" Color="@alertColor" Dismissable="false">
|
||||
<Icon Name="@alertIconName" class="me-2"></Icon>
|
||||
@alertMessage
|
||||
<Button Color="ButtonColor.Primary" @onclick="EndForm">اتمام عملیات</Button>
|
||||
</Alert>
|
||||
|
||||
|
||||
</div>
|
||||
<div class="signin-banner-from-register">
|
||||
<NavLink href="Register">اکانت ندارید؟ <span>ثبت نام</span></NavLink>
|
||||
</div>
|
||||
@@ -122,23 +134,44 @@
|
||||
[SupplyParameterFromForm]
|
||||
public Authentication? Model { get; set; }
|
||||
protected override void OnInitialized() => Model ??= new();
|
||||
// alert
|
||||
AlertColor alertColor = AlertColor.Primary;
|
||||
IconName alertIconName = IconName.CheckCircleFill;
|
||||
bool Hidealert = true;
|
||||
string alertMessage = "";
|
||||
// protected override async Task OnInitializedAsync()
|
||||
// {
|
||||
// var t1 = userinfo;
|
||||
// var t2 = _hc;
|
||||
// await base.OnInitializedAsync();
|
||||
// }
|
||||
}
|
||||
@functions {
|
||||
private void ShowDangerAlert(string msg)
|
||||
{
|
||||
Hidealert = false;
|
||||
alertColor = AlertColor.Danger;
|
||||
alertIconName = IconName.ExclamationTriangleFill;
|
||||
alertMessage = msg;
|
||||
}
|
||||
private async Task EndForm() =>nav.NavigateTo("/");
|
||||
|
||||
|
||||
private async Task OnLoginClick()
|
||||
{
|
||||
var request = await _hc.PostAsJsonAsync("User/authenticate", Model);
|
||||
// if (request.IsSuccessStatusCode)
|
||||
// {
|
||||
// messageStore?.Clear();
|
||||
// var VerificationID = await request.Content.ReadFromJsonAsync<int>();
|
||||
// nav.NavigateTo($"Verification/{VerificationID}");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// var error = await request.Content.ReadFromJsonAsync<List<string>>();
|
||||
// messageStore?.Add(() => model.Mobile, error);
|
||||
var request = await _hc.PostAsJsonAsync("User/authenticate", Model);
|
||||
if (request.IsSuccessStatusCode)
|
||||
{
|
||||
userinfo = await request.Content.ReadFromJsonAsync<UserAuthenticationDTO>();
|
||||
|
||||
_hc.DefaultRequestHeaders.Add("Authorization",
|
||||
$"Bearer {userinfo?.Token}");
|
||||
|
||||
}
|
||||
else if (request.StatusCode==System.Net.HttpStatusCode.NotFound)
|
||||
ShowDangerAlert("کاربری با این مشخصات یافت نشد");
|
||||
|
||||
else ShowDangerAlert("خطای سیستمی");
|
||||
|
||||
// }
|
||||
}
|
||||
}
|
@@ -2,16 +2,18 @@ using Microsoft.AspNetCore.Components.Web;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using Front;
|
||||
using System.Globalization;
|
||||
using Shared.DTOs;
|
||||
|
||||
var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
||||
builder.RootComponents.Add<App>("#app");
|
||||
builder.RootComponents.Add<HeadOutlet>("head::after");
|
||||
builder.Services.AddBlazorBootstrap();
|
||||
builder.Services.AddScoped<UserAuthenticationDTO>();
|
||||
|
||||
|
||||
//builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("https://localhost:7075/api/") });
|
||||
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("https://localhost:7075/api/") });
|
||||
|
||||
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("http://localhost:5271/api/") });
|
||||
//builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("http://localhost:5271/api/") });
|
||||
|
||||
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("fa-Ir");
|
||||
|
||||
|
Reference in New Issue
Block a user