abp 自定义claim

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity;
using Volo.Abp.Security.Claims;
using Volo.Abp.Uow;
using DependencyAttribute = Volo.Abp.DependencyInjection.DependencyAttribute;
 
namespace Hang.Blog.Web
{
    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(AbpUserClaimsPrincipalFactory))]
    public class MyUserClaimsPrincipalFactory :
       AbpUserClaimsPrincipalFactory, ITransientDependency
    {
        private readonly IHttpContextAccessor _httpContext;
 
        public MyUserClaimsPrincipalFactory(
            UserManager<Volo.Abp.Identity.IdentityUser> userManager,
            RoleManager<Volo.Abp.Identity.IdentityRole> roleManager,
            IOptions<IdentityOptions> options,
            IHttpContextAccessor httpContext)
            : base(
                userManager,
                roleManager,
                options)
        {
            _httpContext = httpContext;
        }
 
        [UnitOfWork]
        public override async Task<ClaimsPrincipal> CreateAsync(Volo.Abp.Identity.IdentityUser user)
        {
            var principal = await base.CreateAsync(user);
 
             
            principal.Identities
                .First().AddClaim(new Claim("Test", "Test"));
              return principal;
        }
    }
}
 private async Task CreateApiResourcesAsync()
        {
            var commonApiUserClaims = new[]
            {
                "email",
                "email_verified",
                "name",
                "phone_number",
                "phone_number_verified",
                "role",
                "Test",
 
            };
 
            await CreateApiResourceAsync("Blog", commonApiUserClaims);
        }

 

转 https://blog.csdn.net/hangyejiadao/article/details/113881113

posted @ 2023-02-06 17:15  dreamw  阅读(147)  评论(0)    收藏  举报