75 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C#
		
	
	
using IdentityServer4.Extensions;
 | 
						|
using IdentityServer4.Models;
 | 
						|
using IdentityServer4.Services;
 | 
						|
using IRaCIS.Core.Domain.Models;
 | 
						|
using IRaCIS.Core.Infra.EFCore;
 | 
						|
using Microsoft.Extensions.Logging;
 | 
						|
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Linq;
 | 
						|
using System.Security.Claims;
 | 
						|
using System.Threading.Tasks;
 | 
						|
 | 
						|
namespace IRaCIS.Core.IdentityServer4.Account
 | 
						|
{
 | 
						|
    public class AdminProfileService : IProfileService
 | 
						|
    {
 | 
						|
        private readonly ILogger _logger;
 | 
						|
        private readonly IRepository<User> _userRepository;
 | 
						|
 | 
						|
        public AdminProfileService(
 | 
						|
            ILogger<AdminProfileService> logger,
 | 
						|
            IRepository<User> userRepository
 | 
						|
        )
 | 
						|
        {
 | 
						|
            _logger = logger;
 | 
						|
            _userRepository = userRepository;
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 获得用户信息
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="context"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public virtual  Task GetProfileDataAsync(ProfileDataRequestContext context)
 | 
						|
        {
 | 
						|
            var sub = context.Subject?.GetSubjectId();
 | 
						|
            if (sub == null) throw new Exception("用户Id为空");
 | 
						|
 | 
						|
            var user =  _userRepository.Where(t=>t.Id==Guid.Parse(sub)).FirstOrDefault();
 | 
						|
            if (user == null)
 | 
						|
            {
 | 
						|
                _logger?.LogWarning("用户{0}不存在", sub);
 | 
						|
            }
 | 
						|
            else
 | 
						|
            {
 | 
						|
                var claims = new List<Claim>()
 | 
						|
                {
 | 
						|
                    new Claim(ClaimAttributes.UserName, user.UserName ?? ""),
 | 
						|
                };
 | 
						|
              
 | 
						|
 | 
						|
                context.IssuedClaims = claims;
 | 
						|
            }
 | 
						|
            return Task.CompletedTask;
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 验证用户是否有效
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="context"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public virtual  Task IsActiveAsync(IsActiveContext context)
 | 
						|
        {
 | 
						|
            _logger.LogDebug("用户是否有效:{caller}", context.Caller);
 | 
						|
 | 
						|
            var sub = context.Subject?.GetSubjectId();
 | 
						|
            if (sub == null) throw new Exception("用户Id为空");
 | 
						|
 | 
						|
            var user =  _userRepository.Where(t => t.Id == Guid.Parse(sub)).FirstOrDefault();
 | 
						|
            context.IsActive = user?.Status == 0;
 | 
						|
 | 
						|
            return Task.CompletedTask;
 | 
						|
        }
 | 
						|
    }
 | 
						|
} |