login service by localstorg

This commit is contained in:
mmrbnjd
2024-04-18 18:26:12 +03:30
parent fa5a83d8d3
commit 77e004d090
11 changed files with 241 additions and 58 deletions

View File

@@ -42,7 +42,7 @@ namespace Back.Services
// .ThenInclude(ti => ti.CalculationType)
.FirstOrDefaultAsync();
}
public async Task<UserAuthenticationDTO?> UserAuthentication(string UserNameORUserID, string Password="")
public async Task<UserAuthenticationDTO?> UserAuthentication(string UserNameORUserID, string Password="",bool newtoken=true)
{
UserAuthenticationDTO ret = new UserAuthenticationDTO();
User? user = null;
@@ -54,7 +54,10 @@ namespace Back.Services
if (user == null)
return null;
ret.Token =await CerateToken(user.ID, user.Username);
string Jwt_Lifetime_Minutes = await GetJwt_Lifetime_Minutes();
ret.Token =newtoken ? await CerateToken(user.ID, user.Username, Jwt_Lifetime_Minutes) : user.Token;
ret.FullName = user.Fullname;
ret.Photo = user.Photo==null ? null : Convert.ToBase64String(user.Photo);
//foreach (var rol in user.RolUsers)
@@ -127,6 +130,9 @@ namespace Back.Services
Logo = user.RolUsers.First().Company.Logo == null ? null : Convert.ToBase64String(user.RolUsers.First().Company.Logo)
};
var dt = newtoken ? DateTime.Now : user.DateLastLogin.ToMiladiByTime();
ret.enterDate= dt;
ret.exitDate= dt.AddMinutes(Convert.ToInt32(Jwt_Lifetime_Minutes));
return ret;
}
public async Task<User> AddUser(User item)
@@ -154,7 +160,10 @@ namespace Back.Services
}
public async Task<User?> GetUserByUserID(int UserID)
{
return await _RepoUser.Get(w => w.ID == UserID).FirstOrDefaultAsync();
return await _RepoUser.Get(w => w.ID == UserID)
.Include(ti => ti.RolUsers)
.ThenInclude(ti => ti.Company)
.FirstOrDefaultAsync();
}
public async Task SetTokenAndDateLogininDB(int UserID,string Token)
{
@@ -162,7 +171,7 @@ namespace Back.Services
if (user != null)
{
user.Token = Token;
user.DateLastLogin=DateTime.Now.ConvertMiladiToShamsi();
user.DateLastLogin=DateTime.Now.ConvertMiladiToShamsiByTime();
await _RepoUser.UpdateAsync(user);
}
}
@@ -229,9 +238,9 @@ namespace Back.Services
return await _RepoUser.UpdateByObjAsync(user);
}
//--------internal
private async Task<string> CerateToken(int UserId, string UserName)
private async Task<string> GetJwt_Lifetime_Minutes()
{
string Jwt_Lifetime_Minutes = "";
string Jwt_Lifetime_Minutes = "60";
try
{
Jwt_Lifetime_Minutes = _configuration["Fixedvalues:Jwt_Lifetime_Minutes"].ToString();
@@ -254,6 +263,10 @@ namespace Back.Services
//To DO
}
return Jwt_Lifetime_Minutes;
}
private async Task<string> CerateToken(int UserId, string UserName,string Jwt_Lifetime_Minutes)
{
#region CreateToken
var securityKey = new SymmetricSecurityKey(
Encoding.ASCII.GetBytes(Fixedvalues.SecretForKey)