Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can get custom claim #6048

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

How can get custom claim #6048

journey191 opened this issue Nov 4, 2020 · 5 comments

Comments

@journey191
Copy link

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
Copy link
Member

maliming commented Nov 4, 2020

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

@journey191
Copy link
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 Nov 4, 2020
@kishoresahas
Copy link
Contributor

The above solution is not working in v5.1

@maliming
Copy link
Member

@kishoresahas new issue, please.

@kishoresahas
Copy link
Contributor

@kishoresahas new issue, please.

#7998

Above got it resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants