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

@@ -40,7 +40,17 @@ namespace Back.Common
PersianCalendar PersianCal = new PersianCalendar();
return PersianCal.GetYear(date).ToString("0000") +
PersianCal.GetMonth(date).ToString("00") +
PersianCal.GetDayOfMonth(date).ToString("00");
PersianCal.GetDayOfMonth(date).ToString("00")
;
}
public static string ConvertMiladiToShamsiByTime(this DateTime date)
{
PersianCalendar PersianCal = new PersianCalendar();
return PersianCal.GetYear(date).ToString("0000") +
PersianCal.GetMonth(date).ToString("00") +
PersianCal.GetDayOfMonth(date).ToString("00") +
date.Hour.ToString("00") + date.Minute.ToString("00") + date.Second.ToString("00")
;
}
public static string ShamciToFormatShamci(this string str)
{
@@ -52,6 +62,12 @@ namespace Back.Common
PersianCalendar p = new PersianCalendar();
return p.ToDateTime(Convert.ToInt32(value.Substring(0, 4)), Convert.ToInt32(value.Substring(4, 2)), Convert.ToInt32(value.Substring(6, 2)), 0, 0, 0, 0);
}
public static DateTime ToMiladiByTime(this string value)
{
PersianCalendar p = new PersianCalendar();
return p.ToDateTime(Convert.ToInt32(value.Substring(0, 4)), Convert.ToInt32(value.Substring(4, 2)), Convert.ToInt32(value.Substring(6, 2)), Convert.ToInt32(value.Substring(8, 2)), Convert.ToInt32(value.Substring(10, 2)), Convert.ToInt32(value.Substring(12, 2)), 0);
}
public static async Task<PagingDto<T>> Paging<T>(this IQueryable<T> values, int pageId, int take)
{
if (/*values.Count()<1000 && */pageId == 0 && take == 0)

View File

@@ -26,14 +26,20 @@ namespace Back.Controllers
if (result != null) return Ok(result);
else return NotFound("کاربری با این مشخصات یافت نشد");
}
[HttpGet("test")]
public async Task<ActionResult> test()
[HttpGet("CheckAuthenticate")]
public async Task<ActionResult<UserAuthenticationDTO>> CheckAuthenticate()
{
return Ok();
// var accessToken = Request.Headers["Authorization"].ToString().Split(' ')[1];
var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
var UserID = claim.Value;
var result = await _servUser.UserAuthentication(UserID,newtoken:false);
return Ok(result);
}
}
}

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)