123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using Microsoft.AspNetCore.Authentication;
- using Microsoft.AspNetCore.Authentication.Cookies;
- using Microsoft.AspNetCore.Http;
- using NetTaste;
- using OASystem.API.OAMethodLib.JuHeAPI;
- using OASystem.Domain.Dtos.Business;
- using SqlSugar.Extensions;
- using System.IdentityModel.Tokens.Jwt;
- using System.Security.Claims;
- using static NPOI.HSSF.Util.HSSFColor;
- namespace OASystem.API.OAMethodLib
- {
- public class JWTHelper
- {
- public class JwtHelper
- {
- private readonly static IHttpContextAccessor _httpContext = AutofacIocManager.Instance.GetService<IHttpContextAccessor>();
-
-
-
-
-
- public static async Task<string> IssueJwtAsync(TokenModelJwt tokenModel)
- {
-
- string iss = "OASystem.com";
- string aud = "OASystem.com";
- string secret = AppSettingsHelper.Get("JwtSecurityKey");
- var claims = new List<Claim>
- {
-
-
- new Claim(JwtRegisteredClaimNames.Jti, tokenModel.UserId.ToString()),
- new Claim(JwtRegisteredClaimNames.Name, tokenModel.UserName),
-
- new Claim(JwtRegisteredClaimNames.Iat, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"),
- new Claim(JwtRegisteredClaimNames.Nbf,$"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}") ,
-
- new Claim(JwtRegisteredClaimNames.Exp,$"{new DateTimeOffset(DateTime.Now.AddSeconds(7200)).ToUnixTimeSeconds()}"),
- new Claim(JwtRegisteredClaimNames.Iss,iss),
- new Claim(JwtRegisteredClaimNames.Aud,aud),
-
-
- };
-
- claims.AddRange(tokenModel.Role.Split(',').Select(s => new Claim(ClaimTypes.Role, s)));
-
- var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret));
- var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
- var jwt = new JwtSecurityToken(
- issuer: iss,
- claims: claims,
- signingCredentials: creds
-
- );
- var jwtHandler = new JwtSecurityTokenHandler();
- var encodedJwt = jwtHandler.WriteToken(jwt);
- return encodedJwt;
- }
-
-
-
-
-
- public static TokenModelJwt SerializeJwt(string jwtStr)
- {
- jwtStr = jwtStr.Replace("Bearer ", "");
- if (string.IsNullOrEmpty(jwtStr)) return null;
- var jwtHandler = new JwtSecurityTokenHandler();
- JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(jwtStr);;
- object role,userName;
- try
- {
- jwtToken.Payload.TryGetValue(ClaimTypes.Role, out role);
- jwtToken.Payload.TryGetValue("name", out userName);
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- throw;
- }
- var tm = new TokenModelJwt
- {
- UserId = (jwtToken.Id).ObjToInt(),
- UserName = userName != null ? userName.ObjToString() : "",
- Role = role != null ? role.ObjToString() : "",
- };
- return tm;
- }
- }
-
-
-
- public class TokenModelJwt
- {
-
-
-
- public int UserId { get; set; }
- public string UserName { get; set; }
- public string Role { get; set; } = "Admin";
-
-
-
-
- public int ExpirationTime { get; set; } = 7200;
- }
- }
- }
|