You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.2 KiB
C#

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
{
/// <summary>
/// 获取Claim中存储的值
/// </summary>
public static class ClaimHelper
{
/// <summary>
/// 获取Claim中存储的值
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
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;
}
}
}