2025-07-24 23:18:11 +03:30
|
|
|
|
using Hushian.Application.Constants;
|
|
|
|
|
using Hushian.Application.Models;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using System;
|
2025-07-06 20:38:02 +03:30
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
2025-07-24 23:18:11 +03:30
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2025-07-06 20:38:02 +03:30
|
|
|
|
using System.Linq;
|
2025-07-24 23:18:11 +03:30
|
|
|
|
using System.Security.Claims;
|
2025-07-06 20:38:02 +03:30
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Hushian.Application.Services
|
|
|
|
|
{
|
|
|
|
|
public static class ExMethod
|
|
|
|
|
{
|
|
|
|
|
public static string GetDatePersian(this DateTime d)
|
|
|
|
|
{
|
|
|
|
|
PersianCalendar pc = new PersianCalendar();
|
|
|
|
|
return string.Format("{0}/{1}/{2}", pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d));
|
|
|
|
|
}
|
|
|
|
|
public static string GetTime(this DateTime d)
|
|
|
|
|
{
|
|
|
|
|
return d.Hour.ToString() + ":" + d.Minute.ToString();
|
|
|
|
|
}
|
2025-07-24 23:18:11 +03:30
|
|
|
|
public static JwtSecurityToken GenerateToken(this JwtSettings _jwtSettings,string UserName, int userId, string Role)
|
|
|
|
|
{
|
|
|
|
|
var claims = new[]
|
|
|
|
|
{
|
2025-08-20 14:15:10 +03:30
|
|
|
|
new Claim(JwtRegisteredClaimNames.Sub,
|
2025-08-22 01:02:14 +03:30
|
|
|
|
//Role=="User" ? "U"+userId.ToString()
|
|
|
|
|
//:Role=="Company" ? "C"+userId.ToString()
|
|
|
|
|
//:
|
|
|
|
|
userId.ToString()),
|
2025-07-24 23:18:11 +03:30
|
|
|
|
new Claim(ClaimTypes.NameIdentifier, UserName),
|
|
|
|
|
new Claim(CustomClaimTypes.Uid,userId.ToString()),
|
|
|
|
|
new Claim(ClaimTypes.Role, Role)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var symmetricSecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.Key));
|
|
|
|
|
var signingCredentials = new SigningCredentials(symmetricSecurityKey, SecurityAlgorithms.HmacSha256);
|
|
|
|
|
var jwtSecurityToken = new JwtSecurityToken(
|
|
|
|
|
issuer: _jwtSettings.Issuer,
|
|
|
|
|
audience: _jwtSettings.Audience,
|
|
|
|
|
claims: claims,
|
|
|
|
|
expires: DateTime.UtcNow.AddMinutes(_jwtSettings.DurationInMinutes),
|
|
|
|
|
signingCredentials: signingCredentials);
|
|
|
|
|
|
|
|
|
|
//user.Token = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
|
|
|
|
|
//var resultupdateuser = await _userManager.UpdateAsync(user);
|
|
|
|
|
|
|
|
|
|
return jwtSecurityToken;
|
|
|
|
|
}
|
2025-07-06 20:38:02 +03:30
|
|
|
|
}
|
|
|
|
|
}
|