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;
        }
    }
}