Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs

56 lines
1.9 KiB
C#
Raw Normal View History

2021-10-12 16:52:28 +08:00
using Yi.Framework.Model;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Model.Models;
2022-04-30 21:48:18 +08:00
using System.IdentityModel.Tokens.Jwt;
2021-10-10 17:30:31 +08:00
2021-10-12 16:52:28 +08:00
namespace Yi.Framework.WebCore
{
public static class CommonExtend
{
/// <summary>
/// 判断是否为异步请求
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static bool IsAjaxRequest(this HttpRequest request)
{
string header = request.Headers["X-Requested-With"];
return "XMLHttpRequest".Equals(header);
}
2021-10-10 17:30:31 +08:00
2021-10-12 16:52:28 +08:00
/// <summary>
/// 基于HttpContext,当前鉴权方式解析,获取用户信息
2022-01-11 16:40:15 +08:00
/// 现在使用redis作为缓存不需要将菜单存放至jwt中了
2021-10-12 16:52:28 +08:00
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
2022-04-03 23:21:53 +08:00
public static UserEntity GetCurrentUserEntityInfo(this HttpContext httpContext, out List<Guid> menuIds)
2021-10-12 16:52:28 +08:00
{
2022-04-30 21:48:18 +08:00
IEnumerable<Claim> claimlist = null;
long resId = 0;
try
{
claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
resId = Convert.ToInt64(claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid).Value);
}
catch
{
throw new Exception("未授权Token鉴权失败");
}
2022-04-03 23:21:53 +08:00
menuIds = claimlist.Where(u => u.Type == "menuIds").ToList().Select(u => new Guid(u.Value)).ToList();
return new UserEntity()
2021-10-24 17:13:28 +08:00
{
2022-04-02 17:44:50 +08:00
Id = resId,
2022-04-30 21:48:18 +08:00
//Name = claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Name).Value
2021-10-12 16:52:28 +08:00
};
}
}
}