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

CurrentUser add claim cannot work(cannot go to my CreateAsync function breakpoints at all) and document request #8073

Closed
gerryge opened this issue Mar 16, 2021 · 2 comments

Comments

@gerryge
Copy link
Contributor

gerryge commented Mar 16, 2021

  • Your ABP Framework version
    v4.2.2
  • Your User Interface type (Angular/MVC/React... etc.) if the issue is related to a specific UI
    MVC
  • Your database provider(EF Core/MongoDB)
    EF Core

Description:
I want to add custom claim to CurrentUser And I do as recommendation in bellow link:
#42 (comment)

  • Steps needed to reproduce the problem.
  1. abp new Acme.BookStore --tiered
  2. Add MyUserClaimsPrincipalFactory(detail see below code) class in Acme.BookStore.Domain project
  3. Replace the AbpUserClaimsPrincipalFactory with MyUserClaimsPrincipalFactory by pre-configuring the IdentityBuilder in the PreConfigureServices of BookStoreDomainModule.cs module
  4. Start the Acme.BookStore.IdentityServer project and login in admin/1q2w3E*
  5. Check the Claims and cannot find my custom claim(mpa_app)
namespace Acme.BookStore
{
    public class MyUserClaimsPrincipalFactory : AbpUserClaimsPrincipalFactory, ITransientDependency
    {
        public MyUserClaimsPrincipalFactory(UserManager<IdentityUser> userManager,
            RoleManager<IdentityRole> roleManager,
            IOptions<IdentityOptions> options,
            ICurrentPrincipalAccessor currentPrincipalAccessor,
            IAbpClaimsPrincipalFactory abpClaimsPrincipalFactory) :
            base(userManager, roleManager, options, currentPrincipalAccessor, abpClaimsPrincipalFactory)
        {
        }

        [UnitOfWork]
        public override async Task<ClaimsPrincipal> CreateAsync(IdentityUser user)
        {
            var principal = await base.CreateAsync(user);
            var identity = principal.Identities.First();
            // add custom claim
            identity.AddClaim(new Claim("mpa_app", "app1"));
            identity.AddClaim(new Claim("mpa_app", "app2"));
            identity.AddClaim(new Claim("mpa_app", "app3"));

            return principal;
        }
    }
}

In BookStoreDomainModule.cs

public override void PreConfigureServices(ServiceConfigurationContext context)
{
        PreConfigure<IdentityBuilder>(builder =>
        {
            builder.AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>();
        });
 }

bookstore-tiered.zip

I have uploaded my test demo, I hope you can help me find the problem or what I have missed.
As time goes, does we have another way to extend user claims?
I search all the ticket about the claims, and I found it was very confusing and lots of them should be out-of-date.
If you have documentation that explains how to extend the user claim, how to map them will prefer good.

I am very confusing as below way, which one recommended, and what's difference, and each one for what scenario?
#7165 (comment)
#7998 (comment)
#7973 (comment)

Thank you!
Have a nice day.

@gerryge gerryge changed the title CurrentUser add claim cannot work(cannot go to my CreateAsync function breakpoints at all) CurrentUser add claim cannot work(cannot go to my CreateAsync function breakpoints at all) and document request Mar 16, 2021
@realLiangshiwei
Copy link
Member

For now,

public class MyAbpClaimsPrincipalContributor : IAbpClaimsPrincipalContributor, ITransientDependency
{
    public Task ContributeAsync(AbpClaimsPrincipalContributorContext context)
    {
        var claimsIdentity = new ClaimsIdentity();
        claimsIdentity.AddIfNotContains(new Claim("Organization", "OrganizationValue"));
        context.ClaimsPrincipal.AddIdentity(claimsIdentity);

        return Task.CompletedTask;
    }
}

public class MyAbpClaimsService : AbpClaimsService
{
    public MyAbpClaimsService(IProfileService profile, ILogger<DefaultClaimsService> logger) : base(profile, logger)
    {
    }

    protected override IEnumerable<string> FilterRequestedClaimTypes(IEnumerable<string> claimTypes)
    {
        return base.FilterRequestedClaimTypes(claimTypes)
            .Union(new []{
                AbpClaimTypes.TenantId,
                AbpClaimTypes.EditionId,
                "Organization"
            });
    }
}


context.Services.Replace(ServiceDescriptor.Transient<IClaimsService, MyAbpClaimsService>());

After version 4.3, there is an easier way: #7998

@gerryge
Copy link
Contributor Author

gerryge commented Mar 16, 2021

var claimsIdentity = new ClaimsIdentity(); ???

why not use
var claimsIdentity = context.ClaimsPrincipal.Identities.FirstOrDefault();

@hikalkan
Can you help to clarify?
Best regards.

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

2 participants