From 3ce0355a79184124f603aeac0ec3a0760a05aa6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sun, 24 Oct 2021 15:13:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9jwt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Yi.Framework/Yi.Framework.Core/MakeJwt.cs | 24 +++++++++++++------ .../Yi.Framework.WebCore/CommonExtend.cs | 13 ++++++---- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Yi.Framework/Yi.Framework.Core/MakeJwt.cs b/Yi.Framework/Yi.Framework.Core/MakeJwt.cs index 0730ec2b..f1ad675c 100644 --- a/Yi.Framework/Yi.Framework.Core/MakeJwt.cs +++ b/Yi.Framework/Yi.Framework.Core/MakeJwt.cs @@ -13,28 +13,38 @@ using JwtRegisteredClaimNames = Microsoft.IdentityModel.JsonWebTokens.JwtRegiste namespace Yi.Framework.Core { + + public class jwtUser + { + public user user { get; set; } + public List menuIds { get; set; } + + } + public class MakeJwt { /// - /// user需关联所有roles + /// user需关联所有roles,还有一个menuIds /// /// /// - public static string app(user _user) + public static string app(jwtUser _user) { //通过查询权限,把所有权限加入进令牌中 List claims = new List(); claims.Add(new Claim(JwtRegisteredClaimNames.Nbf, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}")); claims.Add(new Claim(JwtRegisteredClaimNames.Exp, $"{new DateTimeOffset(DateTime.Now.AddMinutes(30)).ToUnixTimeSeconds()}")); - claims.Add(new Claim(ClaimTypes.Name, _user.username)); - claims.Add(new Claim(ClaimTypes.Sid, _user.id.ToString())); - - foreach (var k in _user.roles) + claims.Add(new Claim(ClaimTypes.Name, _user.user.username)); + claims.Add(new Claim(ClaimTypes.Sid, _user.user.id.ToString())); + foreach (var k in _user.menuIds) + { + claims.Add(new Claim("menuIds",k.id.ToString())); + } + foreach (var k in _user.user.roles) { claims.Add(new Claim(ClaimTypes.Role, k.role_name)); } - var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtConst.SecurityKey)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); diff --git a/Yi.Framework/Yi.Framework.WebCore/CommonExtend.cs b/Yi.Framework/Yi.Framework.WebCore/CommonExtend.cs index 9e5a2f12..7ddde91a 100644 --- a/Yi.Framework/Yi.Framework.WebCore/CommonExtend.cs +++ b/Yi.Framework/Yi.Framework.WebCore/CommonExtend.cs @@ -29,14 +29,19 @@ namespace Yi.Framework.WebCore /// /// /// - public static user GetCurrentUserInfo(this HttpContext httpContext) + public static user GetCurrentUserInfo(this HttpContext httpContext, out List menuIds) { IEnumerable claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims; - - Int32.TryParse(claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Sid).Value,out int resId); + + Int32.TryParse(claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Sid).Value, out int resId); + + + menuIds = claimlist.Where(u => u.Type == "menuIds").ToList().Select(u => Convert.ToInt32(u.Value)).ToList(); + + return new user() { - id =resId, + id = resId, username = claimlist.FirstOrDefault(u => u.Type == ClaimTypes.Name).Value ?? "匿名" }; }