How can get custom claim

发布时间 2023-05-29 17:24:54作者: dreamw

@@abp 7.0 openiddict setting token ValidateLifetime
-->https://stackoverflow.com/questions/75408673/how-can-i-change-the-openiddict-accesstoken-lifetime-in-apb-7
##--->thanks , i founded it PreConfigure<OpenIddictServerBuilder>(builder => { builder .SetAccessTokenLifetime(TimeSpan.FromDays(365)) .SetIdentityTokenLifetime(TimeSpan.FromDays(365)); });

@@abp OpenIddict OpenIddictServerBuilder
-->https://github.com/abpframework/abp/blob/dev/docs/en/Modules/OpenIddict.md
-->##https://docs.abp.io/en/abp/latest/Authorization#claims-principal-factory
-->IAbpClaimsPrincipalContributor

@@abp IAbpClaimsPrincipalContributor
https://github.com/abpframework/abp/issues/6048
#How can get custom claim · Issue #6048 · abpframework/abp
#1.Created a class inheriting from the UserClaimsPrincipalFactory class
#2.Replace the UserClaimsPrincipalFactory by pre-configuring
#

 

Closed
 
 

How can get custom claim#6048

journey191 opened this issue on Nov 4, 2020 · 5 comments
 

Comments

@journey191
 
 

I created a class inheriting from the UserClaimsPrincipalFactory class and override the CreateAsync method,it worked.
UX3HN5@(YHGF25KERES
But I can't get my custom claim
S8OW} 1{U%XW9 VL58JS VH

 
 
@maliming
 
Member
  • Your ABP Framework version.
  • Tiered (MVC) or Identity Server Seperated (Angular): yes / no
  • Steps needed to reproduce the problem.
 
@journey191
 
Author

@maliming

  • Your ABP Framework version:3.1
  • Tiered (MVC) or Identity Server Seperated (Angular): no(Angular)
  • Steps needed to reproduce the problem.

1.Created a class inheriting from the UserClaimsPrincipalFactory class

public class MyUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<Volo.Abp.Identity.IdentityUser, Volo.Abp.Identity.IdentityRole>, ITransientDependency
    {
        private readonly IRepository<UserPosition> _userPositionRepository;
        private readonly IHttpContextAccessor _httpContext;

        public MyUserClaimsPrincipalFactory(
           UserManager<Volo.Abp.Identity.IdentityUser> userManager,
           RoleManager<Volo.Abp.Identity.IdentityRole> roleManager,
           IOptions<IdentityOptions> options,
           IRepository<UserPosition> userPositionRepository,
           IHttpContextAccessor httpContext)
           : base(
                 userManager,
                 roleManager,
                 options)
        {
            _userPositionRepository = userPositionRepository;
            _httpContext = httpContext;
        }

        [UnitOfWork]
        public override async Task<ClaimsPrincipal> CreateAsync(Volo.Abp.Identity.IdentityUser user)
        {
            var principal = await base.CreateAsync(user);

            if (user.TenantId.HasValue)
            {
                principal.Identities
                    .First()
                    .AddClaim(new Claim(AbpClaimTypes.TenantId, user.TenantId.ToString()));
            }

            var userPositionList = await _userPositionRepository.Where(_ => _.UserId == user.Id).ToListAsync();
            string positionIds = "";
            foreach (var item in userPositionList)
            {
                positionIds += positionIds == "" ? item.PositionId.ToString() : "," + item.PositionId.ToString();
            }
            principal.Identities
                    .First()
                    .AddClaim(new Claim("PositionIds", positionIds));

            return principal;
        }
    }

2.Replace the UserClaimsPrincipalFactory by pre-configuring

public override void PreConfigureServices(ServiceConfigurationContext context)
        {
            OntallCoreDomainObjectExtensions.Configure();

            PreConfigure<IdentityBuilder>(builder =>
            {
                builder.AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>();
            });
        }

I can't get my custom claim

var positionIds = CurrentUser.FindClaimValue("PositionIds");
// var claims = CurrentUser.GetAllClaims();

But Now I resolve the problem by adding ApiResource in IdentityServerDataSeedContributor.cs

private async Task CreateApiResourcesAsync()
        {
            var commonApiUserClaims = new[]
            {
                "email",
                "email_verified",
                "name",
                "phone_number",
                "phone_number_verified",
                "role",
                "PositionIds"
            };

            await CreateApiResourceAsync("OntallCore", commonApiUserClaims);
        }
 
@maliming maliming closed this as completed on Nov 4, 2020
 
@kishoresahas
 
Contributor

The above solution is not working in v5.1

 
@maliming
 
Member

@kishoresahas new issue, please.

 
@kishoresahas
 
Contributor

@kishoresahas new issue, please.

#7998

Above got it resolved

https://github.com/abpframework/abp/issues/6048