Files
Hushian/Hushian.Application/Services/ExMethod.cs
mmrbnjd c535cb078e ...
2025-08-20 14:15:10 +03:30

56 lines
2.1 KiB
C#

using Hushian.Application.Constants;
using Hushian.Application.Models;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
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();
}
public static JwtSecurityToken GenerateToken(this JwtSettings _jwtSettings,string UserName, int userId, string Role)
{
var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Sub,
Role=="User" ? "U"+userId.ToString()
:Role=="Company" ? "C"+userId.ToString()
:userId.ToString()),
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;
}
}
}