上下文对象获取用户id扩展

This commit is contained in:
陈淳
2022-09-15 18:44:12 +08:00
parent 844a7b455c
commit 52b8bc8909
2 changed files with 11 additions and 5 deletions

View File

@@ -110,7 +110,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
public async Task<Result> GetUserAllInfo()
{
//通过鉴权jwt获取到用户的id
var userId = HttpContext.GetCurrentUserEntityInfo(out _).Id;
var userId = HttpContext.GetUserIdInfo();
var data = await _iUserService.GetUserAllInfo(userId);
data.Menus.Clear();
return Result.Success().SetData(data);
@@ -123,7 +123,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
[HttpGet]
public async Task<Result> GetRouterInfo()
{
var userId = HttpContext.GetCurrentUserEntityInfo(out _).Id;
var userId = HttpContext.GetUserIdInfo();
var data = await _iUserService.GetUserAllInfo(userId);
//将后端菜单转换成前端路由,组件级别需要过滤
@@ -144,7 +144,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
user.Salt = null;
//修改需要赋值上主键哦
user.Id = HttpContext.GetCurrentUserEntityInfo(out _).Id;
user.Id = HttpContext.GetUserIdInfo();
return Result.Success().SetStatus(await _iUserService._repository.UpdateIgnoreNullAsync(user));
}
@@ -156,7 +156,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
[HttpPut]
public async Task<Result> UpdatePassword(UpdatePasswordDto dto)
{
long userId = HttpContext.GetCurrentUserEntityInfo(out _).Id;
long userId = HttpContext.GetUserIdInfo();
if (await _iUserService.UpdatePassword(dto, userId))
{

View File

@@ -26,13 +26,19 @@ namespace Yi.Framework.WebCore
return "XMLHttpRequest".Equals(header);
}
public static long GetUserIdInfo(this HttpContext httpContext)
{
return Convert.ToInt64(httpContext.User.Claims.FirstOrDefault(u => u.Type== JwtRegisteredClaimNames.Sid));
}
/// <summary>
/// 基于HttpContext,当前鉴权方式解析,获取用户信息
/// 现在使用redis作为缓存不需要将菜单存放至jwt中了
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
public static UserEntity GetCurrentUserEntityInfo(this HttpContext httpContext, out List<Guid> menuIds)
public static UserEntity GetUserEntityInfo(this HttpContext httpContext, out List<Guid> menuIds)
{
IEnumerable<Claim> claimlist = null;
long resId = 0;