Open Closed

I have a requirement to save one key for the user and which I want to maintain through out the session of the user #3378


User avatar
0
vipulbuoyancy created
  • ABP Framework version: v5.2.2
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

Hello,

I have a requirement to save one key for the user and which I want to maintain through out the session of the user, so I don't have to call the database multiple times for each and every API request. Please help me how can I achieve this functionality with ABP.io


12 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Did you check this?

    https://docs.abp.io/en/abp/latest/Authorization#claims-principal-factory

  • User Avatar
    0
    vipulbuoyancy created

    Hi Maliming,

    Thanks for your reply,

    Right now I am using below code and which is working fine if I use the api from swagger, but when I call the api from angular project, I am unable to get the claim.

    var identity = _currentPrincipalAccessor.Principal.Identities.FirstOrDefault(); identity.AddClaim(new Claim("VSPCApiKey", result));

    Please help me if you have gone through the similar issue.

    Thanks,

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    So, you add a custom IAbpClaimsPrincipalContributor? Please share your code.

  • User Avatar
    0
    vipulbuoyancy created

    Hi Maliming,

    Thanks for your reply,

    I share my code

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(AbpUserClaimsPrincipalFactory))]
    public class UBXUserClaimsPrincipalFactory : AbpUserClaimsPrincipalFactory, ITransientDependency
    {
            public UBXUserClaimsPrincipalFactory(UserManager<Volo.Abp.Identity.IdentityUser> userManager,                           RoleManager<Volo.Abp.Identity.IdentityRole> roleManager, IOptions<IdentityOptions> options,                        ICurrentPrincipalAccessor currentPrincipalAccessor, IAbpClaimsPrincipalFactory                                     abpClaimsPrincipalFactory) : base(userManager, roleManager, options, currentPrincipalAccessor,                     abpClaimsPrincipalFactory)
            {
            }
            [UnitOfWork]
            public override async Task<ClaimsPrincipal> CreateAsync(Volo.Abp.Identity.IdentityUser user)
            {
            var principal = await base.CreateAsync(user).ConfigureAwait(false);
            principal.Identities
                        .First()
                        .AddClaim(new Claim("ApiKey",  "712bfe60b83028da5XpkbpC6IgW0Fkagi0rWhay1nm7Ru6FzuYx5t6i2GX3PkoN7odDyNL463t5qZvM3WmMqUA67g2syy0rY3Trmhah5JXQXXYargIH4iGu3BObWIWwC"));
                return principal;
            }
    }
    
    public override void PreConfigureServices(ServiceConfigurationContext context)
            {
                PreConfigure<IdentityBuilder>(builder =>
                {
                    builder.AddClaimsPrincipalFactory<UBXUserClaimsPrincipalFactory>();
                });
            }
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Do not override the AbpUserClaimsPrincipalFactory, You can add an IAbpClaimsPrincipalContributor class .

    And add the code to Identity Server project.

  • User Avatar
    0
    vipulbuoyancy created

    hello

    public class SocialSecurityNumberClaimsPrincipalContributor : IAbpClaimsPrincipalContributor, ITransientDependency
    {
        public async Task ContributeAsync(AbpClaimsPrincipalContributorContext context)
        {
            var identity = context.ClaimsPrincipal.Identities.FirstOrDefault();
            var userId = identity?.FindUserId();
            if (userId.HasValue)
            {
                //var userService = context.ServiceProvider.GetRequiredService<IUserService>(); //Your custom service
                //var socialSecurityNumber = await userService.GetSocialSecurityNumberAsync(userId.Value);
                var socialSecurityNumber = "123456789";
                if (socialSecurityNumber != null)
                {
                    identity.AddClaim(new Claim("SocialSecurityNumber", socialSecurityNumber));
                }
            }
        }
    }
    

    I add an IAbpClaimsPrincipalContributor in class. Working fine this code if I use the api from swagger, but when I call the api from angular project, I am unable to get the claim.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    The claims add during the user login process.

    Can you try to re-login?

  • User Avatar
    0
    vipulbuoyancy created

    Hello,

    I try to re-login but I am unable to get the claim.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share a new access_token?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Also try this.

    Configure<AbpClaimsServiceOptions>(options =>
    {
        options.RequestedClaims.AddRange(new[]{ "SocialSecurityNumber" });
    });
    
  • User Avatar
    0
    vipulbuoyanc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JsiSUE9b6sgJmWpEOWMarISwgbODjDeX3YQZY--l8Dgg1lErsceVy2FP-ZhAD0uFzOILqFqTG670_2WzCrNIlysxdHlqG7Srdm7NWKowFHPAtNJfpP7tbCci3j75awd4M-6JM-NFa1IYohbmSpHawFRLep4BNQSCSwvqT6EnRazF7AccF2wfFf2VO2oXn2p1GauGjwSuHb6qfZg7gO_TnASXWJA-Prq_4PNNyxMKmjy65w",

  • User Avatar
    0
    vipulbuoyancy created

    Hello

    Configure<AbpClaimsServiceOptions>(options => { options.RequestedClaims.AddRange(new[]{ "SocialSecurityNumber" }); }); After adding this code working fine.

    Thank you

Made with ❤️ on ABP v9.1.0-preview. Updated on December 10, 2024, 06:38