2024-04-17 15:49:34 +03:30
|
|
|
|
using Back.Common;
|
|
|
|
|
using Back.Data.Contracts;
|
2024-04-29 18:15:46 +03:30
|
|
|
|
using Back.Data.Infrastructure.Repository;
|
2024-04-17 15:49:34 +03:30
|
|
|
|
using Back.Data.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using Shared.DTOs;
|
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2024-04-29 07:58:41 +03:30
|
|
|
|
using System.Reflection;
|
2024-04-17 15:49:34 +03:30
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Back.Services
|
|
|
|
|
{
|
|
|
|
|
public class servUser
|
|
|
|
|
{
|
|
|
|
|
private readonly IConfiguration _configuration;
|
2024-04-18 00:33:46 +03:30
|
|
|
|
|
2024-04-17 15:49:34 +03:30
|
|
|
|
private readonly servNotification _servNotification;
|
|
|
|
|
private readonly IAsyncRepository<User> _RepoUser;
|
2024-04-29 18:15:46 +03:30
|
|
|
|
private readonly RepositoryBase<Company> _RepoCompany;
|
2024-04-17 15:49:34 +03:30
|
|
|
|
private readonly IAsyncRepository<PermissionPeriod> _RepoPermissionPeriod;
|
2024-04-18 00:33:46 +03:30
|
|
|
|
public servUser(IConfiguration configuration
|
|
|
|
|
, servNotification servNotification
|
|
|
|
|
, IAsyncRepository<User> RepoUser
|
2024-04-29 18:15:46 +03:30
|
|
|
|
, IAsyncRepository<PermissionPeriod> RepoPermissionPeriod
|
|
|
|
|
, RepositoryBase<Company> repoCompany)
|
2024-04-17 15:49:34 +03:30
|
|
|
|
{
|
|
|
|
|
_configuration = configuration;
|
|
|
|
|
_servNotification = servNotification;
|
|
|
|
|
_RepoUser = RepoUser;
|
|
|
|
|
_RepoPermissionPeriod = RepoPermissionPeriod;
|
2024-04-29 18:15:46 +03:30
|
|
|
|
_RepoCompany = repoCompany;
|
2024-04-17 15:49:34 +03:30
|
|
|
|
}
|
|
|
|
|
public async Task<User?> GetUserByUserNameAndPassword(string UserName, string Password)
|
|
|
|
|
{
|
|
|
|
|
return await _RepoUser.Get(w => w.Username == UserName && w.Password == Password.encrypted() && w.IsActive)
|
2024-04-18 00:33:46 +03:30
|
|
|
|
// .Include(i => i.RolUsers)
|
|
|
|
|
// .ThenInclude(ti => ti.rolePermissions)
|
|
|
|
|
// .Include(i => i.RolUsers)
|
|
|
|
|
// .ThenInclude(ti=>ti.Company)
|
|
|
|
|
//.ThenInclude(ti => ti.PermissionPeriods)
|
|
|
|
|
// .ThenInclude(ti => ti.Permission)
|
2024-04-29 18:15:46 +03:30
|
|
|
|
.Include(ti => ti.RolUsers)
|
2024-04-17 15:49:34 +03:30
|
|
|
|
.ThenInclude(ti => ti.Company)
|
2024-04-29 18:15:46 +03:30
|
|
|
|
//.ThenInclude(ti => ti.PermissionPeriods)
|
|
|
|
|
// .ThenInclude(ti => ti.CalculationType)
|
2024-04-17 15:49:34 +03:30
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
}
|
2024-04-29 18:15:46 +03:30
|
|
|
|
public async Task<UserAuthenticationDTO?> UserAuthentication(string UserNameORUserID, string Password = "", bool newtoken = true)
|
2024-04-17 15:49:34 +03:30
|
|
|
|
{
|
|
|
|
|
UserAuthenticationDTO ret = new UserAuthenticationDTO();
|
|
|
|
|
User? user = null;
|
|
|
|
|
|
2024-04-29 18:15:46 +03:30
|
|
|
|
if (string.IsNullOrEmpty(Password) && int.TryParse(UserNameORUserID, out int UserID))
|
2024-04-17 15:49:34 +03:30
|
|
|
|
user = await GetUserByUserID(UserID);
|
|
|
|
|
else
|
2024-04-29 18:15:46 +03:30
|
|
|
|
user = await GetUserByUserNameAndPassword(UserNameORUserID, Password);
|
2024-04-17 15:49:34 +03:30
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
return null;
|
2024-04-18 18:26:12 +03:30
|
|
|
|
|
|
|
|
|
string Jwt_Lifetime_Minutes = await GetJwt_Lifetime_Minutes();
|
2024-04-29 07:58:41 +03:30
|
|
|
|
ret.UserName = user.Username;
|
2024-04-29 18:15:46 +03:30
|
|
|
|
ret.Token = newtoken ? await CerateToken(user.ID, user.Username, Jwt_Lifetime_Minutes) : user.Token;
|
2024-04-17 15:49:34 +03:30
|
|
|
|
ret.FullName = user.Fullname;
|
2024-04-29 18:15:46 +03:30
|
|
|
|
ret.Photo = user.Photo == null ? null : Convert.ToBase64String(user.Photo);
|
2024-04-17 17:34:34 +03:30
|
|
|
|
//foreach (var rol in user.RolUsers)
|
|
|
|
|
//{
|
|
|
|
|
// if (!rol.Company.IsActive)
|
|
|
|
|
// continue;
|
|
|
|
|
|
|
|
|
|
// List<PermissionAuthenticationDTO> permissions = new List<PermissionAuthenticationDTO>();
|
|
|
|
|
// foreach (var per in rol.Company.PermissionPeriods)
|
|
|
|
|
// {
|
|
|
|
|
// bool _accessibility = await _checkPermission.AllowPermission(user.ID, rol.CompanyID, per.Permission.ID);
|
2024-04-17 15:49:34 +03:30
|
|
|
|
|
2024-04-17 17:34:34 +03:30
|
|
|
|
// #region Child
|
2024-04-17 15:49:34 +03:30
|
|
|
|
|
2024-04-17 17:34:34 +03:30
|
|
|
|
// List<Permission> Chidpermissions = _accessibility ? await _servPermission.GetChildPermission(per.Permission.ID):new List<Permission>();
|
|
|
|
|
// List<PermissionAuthenticationDTO> ChildpermissionAuthenticationDTOs = new List<PermissionAuthenticationDTO>();
|
|
|
|
|
// foreach (Permission childper in Chidpermissions)
|
|
|
|
|
// {
|
|
|
|
|
// bool _childaccessibility = await _checkPermission.AllowPermission(user.ID, rol.CompanyID, childper.ID);
|
|
|
|
|
// PermissionAuthenticationDTO ChildpermissionAuthenticationDTO = new PermissionAuthenticationDTO
|
|
|
|
|
// {
|
|
|
|
|
// ID = childper.ID,
|
|
|
|
|
// ParentID = childper.ParentID,
|
|
|
|
|
// Title = childper.Title,
|
|
|
|
|
// accessibility = _childaccessibility,
|
|
|
|
|
// //TODO
|
|
|
|
|
// ChildPermissions = null
|
|
|
|
|
// };
|
|
|
|
|
// ChildpermissionAuthenticationDTOs.Add(ChildpermissionAuthenticationDTO);
|
|
|
|
|
// }
|
|
|
|
|
// #endregion
|
2024-04-17 15:49:34 +03:30
|
|
|
|
|
2024-04-17 17:34:34 +03:30
|
|
|
|
// PermissionAuthenticationDTO permissionAuthenticationDTO = new PermissionAuthenticationDTO
|
|
|
|
|
// {
|
|
|
|
|
// ID = per.Permission.ID,
|
|
|
|
|
// ParentID = per.Permission.ParentID,
|
|
|
|
|
// Title = per.Permission.Title,
|
|
|
|
|
// accessibility = _accessibility,
|
|
|
|
|
// //TODO
|
|
|
|
|
// ChildPermissions = ChildpermissionAuthenticationDTOs
|
2024-04-17 15:49:34 +03:30
|
|
|
|
|
2024-04-17 17:34:34 +03:30
|
|
|
|
// //Period=new PeriodDTO()
|
|
|
|
|
// //{
|
|
|
|
|
// // CalculationTypeID = rol.Company.PermissionPeriods.Where(w => w.PermissionID == per.ID).Select(s => s.CalculationType.ID).FirstOrDefault(),
|
|
|
|
|
// // CalculationTypeTitle = rol.Company.PermissionPeriods.Where(w => w.PermissionID == per.ID).Select(s => s.CalculationType.Title).FirstOrDefault(),
|
|
|
|
|
// // RemainingAmount = rol.Company.PermissionPeriods.Where(w => w.PermissionID == per.ID).Select(s => s.RemainingAmount).FirstOrDefault(),
|
|
|
|
|
// // TotalAmount = rol.Company.PermissionPeriods.Where(w => w.PermissionID == per.ID).Select(s => s.TotalAmount).FirstOrDefault()
|
|
|
|
|
// //}
|
2024-04-17 15:49:34 +03:30
|
|
|
|
|
2024-04-17 17:34:34 +03:30
|
|
|
|
// };
|
|
|
|
|
// permissions.Add(permissionAuthenticationDTO);
|
|
|
|
|
// }
|
2024-04-17 15:49:34 +03:30
|
|
|
|
|
2024-04-17 17:34:34 +03:30
|
|
|
|
// ret.Companies.Add(new CompanyAuthenticationDTO
|
|
|
|
|
// {
|
|
|
|
|
// ID = rol.CompanyID,
|
|
|
|
|
// Name = rol.Company.Name,
|
|
|
|
|
// IsAdmin = rol.IsAdmin,
|
|
|
|
|
// Logo= rol.Company.Logo == null ? null : Convert.ToBase64String(rol.Company.Logo)
|
|
|
|
|
|
|
|
|
|
// /*, permissions = permissions*/
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
ret.Company = new CompanyAuthenticationDTO
|
|
|
|
|
{
|
|
|
|
|
ID = user.RolUsers.First().ID,
|
|
|
|
|
Name = user.RolUsers.First().Company.Name,
|
|
|
|
|
IsAdmin = user.RolUsers.First().IsAdmin,
|
|
|
|
|
Logo = user.RolUsers.First().Company.Logo == null ? null : Convert.ToBase64String(user.RolUsers.First().Company.Logo)
|
2024-04-17 15:49:34 +03:30
|
|
|
|
|
2024-04-17 17:34:34 +03:30
|
|
|
|
};
|
2024-04-18 18:26:12 +03:30
|
|
|
|
var dt = newtoken ? DateTime.Now : user.DateLastLogin.ToMiladiByTime();
|
2024-04-29 18:15:46 +03:30
|
|
|
|
ret.enterDate = dt;
|
|
|
|
|
ret.exitDate = dt.AddMinutes(Convert.ToInt32(Jwt_Lifetime_Minutes));
|
2024-04-17 15:49:34 +03:30
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
public async Task<User> AddUser(User item)
|
|
|
|
|
{
|
|
|
|
|
//_contextMongodb.InsertItem(new SysLog()
|
|
|
|
|
//{
|
|
|
|
|
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
|
|
|
|
|
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
|
|
|
|
|
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddUser",
|
|
|
|
|
// Value = JsonConvert.SerializeObject(item),
|
|
|
|
|
// Route = _httpContextAccessor.HttpContext.Request.Path,
|
|
|
|
|
// Type = "User"
|
|
|
|
|
//});
|
|
|
|
|
return await _RepoUser.AddAsync(item);
|
|
|
|
|
}
|
|
|
|
|
public async Task<bool> ExistUser(string UserName)
|
|
|
|
|
{
|
2024-04-29 18:15:46 +03:30
|
|
|
|
|
|
|
|
|
return await _RepoUser.GetAll().AnyAsync(w => w.Username == UserName);
|
|
|
|
|
|
2024-04-17 15:49:34 +03:30
|
|
|
|
}
|
|
|
|
|
public async Task<User> GetUserByUsername(string UserName)
|
|
|
|
|
{
|
|
|
|
|
return await _RepoUser.Get(w => w.Username == UserName).FirstOrDefaultAsync();
|
|
|
|
|
}
|
|
|
|
|
public async Task<User?> GetUserByUserID(int UserID)
|
|
|
|
|
{
|
2024-04-18 18:26:12 +03:30
|
|
|
|
return await _RepoUser.Get(w => w.ID == UserID)
|
|
|
|
|
.Include(ti => ti.RolUsers)
|
|
|
|
|
.ThenInclude(ti => ti.Company)
|
|
|
|
|
.FirstOrDefaultAsync();
|
2024-04-17 15:49:34 +03:30
|
|
|
|
}
|
2024-04-29 18:15:46 +03:30
|
|
|
|
public async Task SetTokenAndDateLogininDB(int UserID, string Token)
|
2024-04-17 15:49:34 +03:30
|
|
|
|
{
|
|
|
|
|
var user = await GetUserByUserID(UserID);
|
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
|
|
|
|
user.Token = Token;
|
2024-04-29 18:15:46 +03:30
|
|
|
|
user.DateLastLogin = DateTime.Now.ConvertMiladiToShamsiByTime();
|
|
|
|
|
await _RepoUser.UpdateAsync(user);
|
2024-04-17 15:49:34 +03:30
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-29 07:58:41 +03:30
|
|
|
|
public async Task<bool> ChangePasswordByMobile(string mobile, string newpassword)
|
|
|
|
|
{
|
2024-04-29 18:15:46 +03:30
|
|
|
|
var user = await GetUserByUsername(mobile);
|
2024-04-29 07:58:41 +03:30
|
|
|
|
if (user == null)
|
|
|
|
|
return false;
|
|
|
|
|
user.Password = newpassword.encrypted();
|
2024-04-29 18:15:46 +03:30
|
|
|
|
return await _RepoUser.UpdateAsync(user);
|
2024-04-29 07:58:41 +03:30
|
|
|
|
}
|
2024-04-29 18:15:46 +03:30
|
|
|
|
public async Task<DashBoardDTO> GetDashBoard(int CompanyID, int UserID)
|
2024-04-17 15:49:34 +03:30
|
|
|
|
{
|
2024-04-29 18:15:46 +03:30
|
|
|
|
DashBoardDTO request = new DashBoardDTO();
|
|
|
|
|
var period = await _RepoPermissionPeriod
|
|
|
|
|
.Get(w => w.CompanyID == CompanyID && (!w.IsLocked.HasValue || !w.IsLocked.Value)).ToListAsync();
|
2024-04-17 15:49:34 +03:30
|
|
|
|
foreach (var item in period)
|
|
|
|
|
{
|
|
|
|
|
request.AlistofServices.Add(new ServiceInDashBoardDTO
|
|
|
|
|
{
|
2024-04-29 18:15:46 +03:30
|
|
|
|
PermissionID = item.PermissionID,
|
2024-04-17 15:49:34 +03:30
|
|
|
|
PermissionName = item.Permission.Title,
|
2024-04-29 18:15:46 +03:30
|
|
|
|
CalTypeID = item.CalculationTypeID,
|
2024-04-17 15:49:34 +03:30
|
|
|
|
CalTypeTitle = item.CalculationType.Title,
|
2024-04-29 18:15:46 +03:30
|
|
|
|
Total = item.CalculationTypeID == 1 ? item.TotalAmount.ToString() : "",
|
2024-04-17 15:49:34 +03:30
|
|
|
|
Remaining = item.CalculationTypeID == 1 ? item.RemainingAmount.ToString()
|
2024-04-29 18:15:46 +03:30
|
|
|
|
: item.CalculationTypeID == 2 ? "" : item.RemainingAmount.ToString().ShamciToFormatShamci()
|
2024-04-17 15:49:34 +03:30
|
|
|
|
});
|
|
|
|
|
}
|
2024-04-29 18:15:46 +03:30
|
|
|
|
request.LastLoginDate = _RepoUser.Get(w => w.ID == UserID).Select(s => s.DateLastLogin).FirstOrDefault();
|
|
|
|
|
if (!string.IsNullOrEmpty(request.LastLoginDate))
|
2024-04-17 15:49:34 +03:30
|
|
|
|
request.LastLoginDate.ShamciToFormatShamci();
|
|
|
|
|
|
|
|
|
|
var user = await GetUserByUserID(UserID);
|
|
|
|
|
if (user.Mobile == user.Username)
|
2024-04-29 18:15:46 +03:30
|
|
|
|
request.Warning.Add(new AlertDTO { Status = 0, Message = "موبایل و نام کاربری بهتر است شبیه هم نباشند" });
|
2024-04-17 15:49:34 +03:30
|
|
|
|
if (user.Mobile.encrypted() == user.Password)
|
|
|
|
|
request.Warning.Add(new AlertDTO { Status = 0, Message = "موبایل و کلمه عبور بهتر است شبیه هم نباشند" });
|
2024-04-29 18:15:46 +03:30
|
|
|
|
var Company = user.RolUsers.Where(w => w.CompanyID == CompanyID).Select(s => s.Company).FirstOrDefault();
|
|
|
|
|
if (Company != null)
|
2024-04-17 15:49:34 +03:30
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(Company.Email) || string.IsNullOrEmpty(Company.Phone)
|
|
|
|
|
|| string.IsNullOrEmpty(Company.EconomicCode) || string.IsNullOrEmpty(Company.UniqeMemory)
|
|
|
|
|
|| string.IsNullOrEmpty(Company.PrivateKey))
|
|
|
|
|
{
|
|
|
|
|
request.Warning.Add(new AlertDTO { Status = 0, Message = "بهتر است اطلاعات شرکت بروزرسانی شود" });
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-29 18:15:46 +03:30
|
|
|
|
var nots = await _servNotification.GetNotifications();
|
2024-04-17 15:49:34 +03:30
|
|
|
|
if (nots.Any())
|
2024-04-29 18:15:46 +03:30
|
|
|
|
request.Notifications = nots.Select(s => new AlertDTO
|
2024-04-17 15:49:34 +03:30
|
|
|
|
{
|
2024-04-29 18:15:46 +03:30
|
|
|
|
Message = s.Message,
|
|
|
|
|
Status = s.Type,
|
|
|
|
|
Path = s.Path,
|
|
|
|
|
ViewSize = s.ViewSize
|
|
|
|
|
}).ToList();
|
2024-04-17 15:49:34 +03:30
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
public async Task<User> UpdateUser(User user)
|
|
|
|
|
{
|
|
|
|
|
//_contextMongodb.InsertItem(new SysLog()
|
|
|
|
|
//{
|
|
|
|
|
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
|
|
|
|
|
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
|
|
|
|
|
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/UpdateUser",
|
|
|
|
|
// Value = JsonConvert.SerializeObject(user),
|
|
|
|
|
// Route = _httpContextAccessor.HttpContext.Request.Path,
|
|
|
|
|
// Type = "User"
|
|
|
|
|
//});
|
|
|
|
|
return await _RepoUser.UpdateByObjAsync(user);
|
|
|
|
|
}
|
2024-04-29 07:58:41 +03:30
|
|
|
|
public async Task<bool> ChangePassword(string newPass, int UserID)
|
|
|
|
|
{
|
|
|
|
|
var user = await GetUserByUserID(UserID);
|
|
|
|
|
if (user == null)
|
|
|
|
|
return false;
|
|
|
|
|
user.Password = newPass.encrypted();
|
|
|
|
|
return await _RepoUser.UpdateAsync(user);
|
|
|
|
|
}
|
2024-04-29 18:15:46 +03:30
|
|
|
|
public async Task<bool> ChangeUserName(string newUserName, int UserID)
|
|
|
|
|
{
|
|
|
|
|
var user = await GetUserByUserID(UserID);
|
|
|
|
|
if (user == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
2024-05-01 15:42:21 +03:30
|
|
|
|
using var transaction =await _RepoCompany._dbContext.Database.BeginTransactionAsync();
|
2024-04-29 18:15:46 +03:30
|
|
|
|
var company = user.RolUsers.First().Company;
|
|
|
|
|
company.Mobile = newUserName;
|
|
|
|
|
if (await _RepoCompany.UpdateAsync(company))
|
|
|
|
|
{
|
|
|
|
|
user.Username = newUserName;
|
|
|
|
|
user.Mobile = newUserName;
|
|
|
|
|
if (await _RepoUser.UpdateAsync(user))
|
|
|
|
|
{
|
2024-05-01 15:42:21 +03:30
|
|
|
|
await transaction.CommitAsync();
|
2024-04-29 18:15:46 +03:30
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-05-01 15:42:21 +03:30
|
|
|
|
{
|
|
|
|
|
await transaction.RollbackAsync();
|
2024-04-29 18:15:46 +03:30
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// return Ok(await _servCompany.AddORUpdateCompanyBoolResult(company));
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public async Task<bool> PermissionChangePassword(string oldPass, int UserID)
|
|
|
|
|
{
|
|
|
|
|
return await _RepoUser.GetAll().AnyAsync(w => w.ID == UserID && w.Password == oldPass.encrypted() && w.IsActive);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public async Task<bool> ExistMobileInUser(string mobile, bool IsActive = true)
|
2024-04-29 07:58:41 +03:30
|
|
|
|
{
|
2024-04-29 18:15:46 +03:30
|
|
|
|
var resquest = _RepoUser.Get(w => w.Mobile == mobile || w.Username == mobile);
|
|
|
|
|
if (IsActive)
|
|
|
|
|
resquest = resquest.Where(w => w.IsActive);
|
2024-04-29 07:58:41 +03:30
|
|
|
|
|
2024-04-29 18:15:46 +03:30
|
|
|
|
return await resquest.AnyAsync();
|
2024-04-29 07:58:41 +03:30
|
|
|
|
}
|
2024-04-17 15:49:34 +03:30
|
|
|
|
//--------internal
|
2024-04-18 18:26:12 +03:30
|
|
|
|
private async Task<string> GetJwt_Lifetime_Minutes()
|
2024-04-17 15:49:34 +03:30
|
|
|
|
{
|
2024-04-18 18:26:12 +03:30
|
|
|
|
string Jwt_Lifetime_Minutes = "60";
|
2024-04-17 15:49:34 +03:30
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Jwt_Lifetime_Minutes = _configuration["Fixedvalues:Jwt_Lifetime_Minutes"].ToString();
|
|
|
|
|
if (string.IsNullOrEmpty(Jwt_Lifetime_Minutes))
|
|
|
|
|
Jwt_Lifetime_Minutes = "60";
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
//SysLog log = new SysLog()
|
|
|
|
|
//{
|
|
|
|
|
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
|
|
|
|
|
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
|
|
|
|
|
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/CerateToken",
|
|
|
|
|
// Value = ex.Message,
|
|
|
|
|
// Route = _httpContextAccessor.HttpContext.Request.Path,
|
|
|
|
|
// Type = "catch"
|
|
|
|
|
//};
|
|
|
|
|
//_contextMongodb.InsertItem(log);
|
|
|
|
|
Jwt_Lifetime_Minutes = "60";
|
|
|
|
|
//To DO
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-18 18:26:12 +03:30
|
|
|
|
return Jwt_Lifetime_Minutes;
|
|
|
|
|
}
|
2024-04-29 18:15:46 +03:30
|
|
|
|
private async Task<string> CerateToken(int UserId, string UserName, string Jwt_Lifetime_Minutes)
|
2024-04-18 18:26:12 +03:30
|
|
|
|
{
|
2024-04-17 15:49:34 +03:30
|
|
|
|
#region CreateToken
|
|
|
|
|
var securityKey = new SymmetricSecurityKey(
|
|
|
|
|
Encoding.ASCII.GetBytes(Fixedvalues.SecretForKey)
|
|
|
|
|
);
|
|
|
|
|
var signingCredentials = new SigningCredentials(
|
|
|
|
|
securityKey, SecurityAlgorithms.HmacSha256
|
|
|
|
|
);
|
|
|
|
|
var claimsForToken = new List<Claim>();
|
|
|
|
|
claimsForToken.Add(new Claim("UserID", UserId.ToString()));
|
|
|
|
|
claimsForToken.Add(new Claim(ClaimTypes.NameIdentifier, UserName));
|
|
|
|
|
|
|
|
|
|
var jwtSecurityToke = new JwtSecurityToken(
|
|
|
|
|
Fixedvalues.Issuer, Fixedvalues.Audience, claimsForToken,
|
|
|
|
|
DateTime.Now, DateTime.Now.AddMinutes(Convert.ToInt32(Jwt_Lifetime_Minutes)), signingCredentials);
|
|
|
|
|
|
2024-04-29 18:15:46 +03:30
|
|
|
|
|
2024-04-17 15:49:34 +03:30
|
|
|
|
string Token = new JwtSecurityTokenHandler()
|
|
|
|
|
.WriteToken(jwtSecurityToke);
|
2024-04-29 18:15:46 +03:30
|
|
|
|
await SetTokenAndDateLogininDB(UserId, Token);
|
2024-04-17 15:49:34 +03:30
|
|
|
|
//_contextMongodb.InsertItem(new SysLog()
|
|
|
|
|
//{
|
|
|
|
|
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
|
|
|
|
|
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
|
|
|
|
|
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/CerateToken",
|
|
|
|
|
// Value = UserId + " " + UserName+"=> "+Token,
|
|
|
|
|
// Route = _httpContextAccessor.HttpContext.Request.Path,
|
|
|
|
|
// Type = "User"
|
|
|
|
|
//});
|
|
|
|
|
return Token;
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|