|
@@ -1,4 +1,9 @@
|
|
|
-using SqlSugar.Extensions;
|
|
|
+using Microsoft.AspNetCore.Authentication;
|
|
|
+using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
+using OASystem.API.OAMethodLib.JuHeAPI;
|
|
|
+using OASystem.Domain.Dtos.Business;
|
|
|
+using SqlSugar.Extensions;
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
using System.Security.Claims;
|
|
|
|
|
@@ -9,17 +14,19 @@ namespace OASystem.API.OAMethodLib
|
|
|
public class JwtHelper
|
|
|
{
|
|
|
|
|
|
+
|
|
|
+ private readonly static IHttpContextAccessor _httpContextAccessor = AutofacIocManager.Instance.GetService<IHttpContextAccessor>();
|
|
|
/// <summary>
|
|
|
/// 颁发JWT字符串
|
|
|
/// </summary>
|
|
|
/// <param name="tokenModel"></param>
|
|
|
/// <returns></returns>
|
|
|
- public static string IssueJwt(TokenModelJwt tokenModel)
|
|
|
+ public static async Task<string> IssueJwtAsync(TokenModelJwt tokenModel)
|
|
|
{
|
|
|
// appsettign.json 操作类
|
|
|
- string iss = "Issuer";
|
|
|
- string aud = "Audience";
|
|
|
- string secret = "Audience";
|
|
|
+ string iss = "OASystem.com";
|
|
|
+ string aud = "OASystem.com";
|
|
|
+ string secret = AppSettingsHelper.Get("JwtSecurityKey");
|
|
|
|
|
|
var claims = new List<Claim>
|
|
|
{
|
|
@@ -29,11 +36,12 @@ namespace OASystem.API.OAMethodLib
|
|
|
2、你也可以研究下 HttpContext.User.Claims ,具体的你可以看看 Policys/PermissionHandler.cs 类中是如何使用的。
|
|
|
*/
|
|
|
|
|
|
- new Claim(JwtRegisteredClaimNames.Jti, tokenModel.Uid.ToString()),
|
|
|
+ new Claim(JwtRegisteredClaimNames.Jti, tokenModel.UserId.ToString()),
|
|
|
+ new Claim(JwtRegisteredClaimNames.GivenName, tokenModel.UserName),
|
|
|
new Claim(JwtRegisteredClaimNames.Iat, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"),
|
|
|
new Claim(JwtRegisteredClaimNames.Nbf,$"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}") ,
|
|
|
//这个就是过期时间,目前是过期7200秒,可自定义,注意JWT有自己的缓冲过期时间
|
|
|
- new Claim (JwtRegisteredClaimNames.Exp,$"{new DateTimeOffset(DateTime.Now.AddSeconds(7200)).ToUnixTimeSeconds()}"),
|
|
|
+ new Claim(JwtRegisteredClaimNames.Exp,$"{new DateTimeOffset(DateTime.Now.AddSeconds(7200)).ToUnixTimeSeconds()}"),
|
|
|
new Claim(JwtRegisteredClaimNames.Iss,iss),
|
|
|
new Claim(JwtRegisteredClaimNames.Aud,aud),
|
|
|
|
|
@@ -55,6 +63,12 @@ namespace OASystem.API.OAMethodLib
|
|
|
//,expires:DateTime.Now.AddMinutes(1)
|
|
|
);
|
|
|
|
|
|
+ // var indentity = new ClaimsIdentity(claims, "FMGJ-OASystem");
|
|
|
+ // var principal = new ClaimsPrincipal(indentity);
|
|
|
+
|
|
|
+ //await _httpContextAccessor.HttpContext?.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
|
|
|
+
|
|
|
+
|
|
|
var jwtHandler = new JwtSecurityTokenHandler();
|
|
|
var encodedJwt = jwtHandler.WriteToken(jwt);
|
|
|
|
|
@@ -70,10 +84,11 @@ namespace OASystem.API.OAMethodLib
|
|
|
{
|
|
|
var jwtHandler = new JwtSecurityTokenHandler();
|
|
|
JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(jwtStr);
|
|
|
- object role;
|
|
|
+ object role,userName;
|
|
|
try
|
|
|
{
|
|
|
jwtToken.Payload.TryGetValue(ClaimTypes.Role, out role);
|
|
|
+ jwtToken.Payload.TryGetValue(ClaimTypes.GivenName, out userName);
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
@@ -82,7 +97,8 @@ namespace OASystem.API.OAMethodLib
|
|
|
}
|
|
|
var tm = new TokenModelJwt
|
|
|
{
|
|
|
- Uid = (jwtToken.Id).ObjToInt(),
|
|
|
+ UserId = (jwtToken.Id).ObjToInt(),
|
|
|
+ UserName = userName != null ? userName.ObjToString() : "",
|
|
|
Role = role != null ? role.ObjToString() : "",
|
|
|
};
|
|
|
return tm;
|
|
@@ -97,20 +113,17 @@ namespace OASystem.API.OAMethodLib
|
|
|
/// <summary>
|
|
|
/// Id
|
|
|
/// </summary>
|
|
|
- public long Uid { get; set; }
|
|
|
+ public int UserId { get; set; }
|
|
|
|
|
|
public string UserName { get; set; }
|
|
|
|
|
|
+ public string Role { get; set; } = "Admin";
|
|
|
+
|
|
|
/// <summary>
|
|
|
- /// 角色
|
|
|
- /// </summary>
|
|
|
- public string Role { get; set; }
|
|
|
- /// <summary>
|
|
|
- /// 职能
|
|
|
+ /// 过期时间,默认过期7200秒
|
|
|
+ /// 注意JWT有自己的缓冲过期时间
|
|
|
/// </summary>
|
|
|
- public string Work { get; set; }
|
|
|
-
|
|
|
-
|
|
|
+ public int ExpirationTime { get; set; } = 7200;
|
|
|
|
|
|
}
|
|
|
}
|