You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.9 KiB
55 lines
1.9 KiB
3 weeks ago
|
using Microsoft.AspNetCore.Http;
|
||
|
using Microsoft.AspNetCore.Mvc;
|
||
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
|
||
|
namespace Wood.Util.JwtAuthorization
|
||
|
{
|
||
|
public static class HttpContextExtension
|
||
|
{
|
||
|
public static JwtUserInfo? UserInfo(this IHttpContextAccessor httpContextAccessor)
|
||
|
{
|
||
|
if (httpContextAccessor?.HttpContext?.User != null)
|
||
|
{
|
||
|
return httpContextAccessor.HttpContext.UserInfo();
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
|
||
|
public static JwtUserInfo? UserInfo(this HttpContext httpContext)
|
||
|
{
|
||
|
if (httpContext?.User != null)
|
||
|
{
|
||
|
if (httpContext.User.Claims.Any())
|
||
|
{
|
||
|
var claims = httpContext.User.Claims;
|
||
|
JwtUserInfo info = new JwtUserInfo();
|
||
|
info.RealName = claims.First(it => it.Type == nameof(info.RealName)).Value;
|
||
|
info.UserName = claims.First(it => it.Type == nameof(info.UserName)).Value;
|
||
|
info.UserId = claims.First(it => it.Type == nameof(info.UserId)).Value.ToLong();
|
||
|
info.AccountType = claims.First(it => it.Type == nameof(info.AccountType)).Value.ToInt();
|
||
|
info.NickName = claims.First(it => it.Type == nameof(info.NickName)).Value;
|
||
|
info.OrgId = claims.First(it => it.Type == nameof(info.OrgId)).Value.ToLong();
|
||
|
info.TenantId = claims.First(it => it.Type == nameof(info.TenantId)).Value.ToLong();
|
||
|
return info;
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public static JwtUserInfo? UserInfo(this ControllerBase controllerBase)
|
||
|
{
|
||
|
if (controllerBase?.HttpContext != null)
|
||
|
{
|
||
|
return controllerBase?.HttpContext.UserInfo();
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
}
|