using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Security.Claims; using System.Text; namespace Admin.Core.Common { /// /// 获取Claim中存储的值 /// public static class ClaimHelper { /// /// 获取Claim中存储的值 /// /// /// public static string Get(HttpContext httpContext, string type) { var schemeProvider = httpContext.RequestServices.GetService(typeof(IAuthenticationSchemeProvider)) as IAuthenticationSchemeProvider; var defaultAuthenticate = schemeProvider.GetDefaultAuthenticateSchemeAsync().Result; if (defaultAuthenticate != null) { var result = httpContext.AuthenticateAsync(defaultAuthenticate.Name).Result; var user = result?.Principal; if (user != null) { return user.FindFirst(type).Value; } } return string.Empty; } } }