修改jwt
This commit is contained in:
@@ -13,28 +13,38 @@ using JwtRegisteredClaimNames = Microsoft.IdentityModel.JsonWebTokens.JwtRegiste
|
||||
|
||||
namespace Yi.Framework.Core
|
||||
{
|
||||
|
||||
public class jwtUser
|
||||
{
|
||||
public user user { get; set; }
|
||||
public List<menu> menuIds { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class MakeJwt
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// user需关联所有roles
|
||||
/// user需关联所有roles,还有一个menuIds
|
||||
/// </summary>
|
||||
/// <param name="_user"></param>
|
||||
/// <returns></returns>
|
||||
public static string app(user _user)
|
||||
public static string app(jwtUser _user)
|
||||
{
|
||||
//通过查询权限,把所有权限加入进令牌中
|
||||
List<Claim> claims = new List<Claim>();
|
||||
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);
|
||||
|
||||
|
||||
@@ -29,14 +29,19 @@ namespace Yi.Framework.WebCore
|
||||
/// </summary>
|
||||
/// <param name="httpContext"></param>
|
||||
/// <returns></returns>
|
||||
public static user GetCurrentUserInfo(this HttpContext httpContext)
|
||||
public static user GetCurrentUserInfo(this HttpContext httpContext, out List<int> menuIds)
|
||||
{
|
||||
IEnumerable<Claim> 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 ?? "匿名"
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user