Ends in:
2 DAYS
4 HRS
20 MIN
30 SEC
Ends in:
2 D
4 H
20 M
30 S
Open Closed

Add more user properties #8394


User avatar
0
duongdangquoc@gmail.com created
  • ABP Framework version: v0.91
  • UI Type: MVC
  • Database System: EF Core (SQL Server) / MongoDB
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

I have added more properties (in the IndentityService project) to the AbpUser table with guide: https://abp.io/docs/latest/framework/architecture/modularity/extending/customizing-application-modules-extending-entities#entity-extensions-ef-core These fields have been created in DB.

Now, I want to get these fields value and add to Claims by overriding the CreateAsync() method. This is my code (in AuthServer project):

public class ExtendAbpUserClaimsPrincipalFactory : AbpUserClaimsPrincipalFactory, ITransientDependency
{
    public ExtendAbpUserClaimsPrincipalFactory(
        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);
        var identity = principal.Identities.First();
        var nodeId = user.GetProperty<long?>(DOCSIdentityConsts.NodeIdClaim);
        var userType = user.GetProperty<long?>(DOCSIdentityConsts.UserTypeClaim);

        var claims = identity.Claims;
        if (!claims.Any(x => x.Type == DOCSIdentityConsts.UserNameClaim))
        {
            identity.AddClaim(new Claim(DOCSIdentityConsts.UserNameClaim, user?.UserName ?? ""));
        }
        if (!claims.Any(x => x.Type == DOCSIdentityConsts.FullNameClaim))
        {
            identity.AddClaim(new Claim(DOCSIdentityConsts.FullNameClaim, user?.Name ?? ""));
        }
        if (!claims.Any(x => x.Type == DOCSIdentityConsts.NodeIdClaim))
        {
            identity.AddClaim(new Claim(DOCSIdentityConsts.NodeIdClaim, nodeId is null ? $"" : nodeId.ToString()));
        }
        if (!claims.Any(x => x.Type == DOCSIdentityConsts.UserTypeClaim))
        {
            identity.AddClaim(new Claim(DOCSIdentityConsts.UserTypeClaim, userType is null ? "" : userType.ToString()));
        }

        Console.WriteLine("IdentityService - nodeId: " + nodeId);
        Debug.Assert(true, "IdentityService - nodeId: " + nodeId);

        return principal;
    }
}

public static class DOCSIdentityConsts
{
    public const string UserNameClaim = "UserName";
    public const string FullNameClaim = "FullName";
    public const string NodeIdClaim = "NodeId";
    public const string UserTypeClaim = "UserType";
}

But my extend fields always return null `var nodeId = user.GetProperty<long?>(DOCSIdentityConsts.NodeIdClaim);

var userType = user.GetProperty<long?>(DOCSIdentityConsts.UserTypeClaim);`

How I can get correct data? And I want to add these fields in the UI for CRUD actions.

Please help.

Many thanks.


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

    hi

    Can you move the ObjectExtensionManager.Instance.MapEfCoreProperty<IdentityUser, string> code to a common module and reference it in identity server and authserver projects?

  • User Avatar
    0
    duongdangquoc@gmail.com created

    Yeah, let me try.

    Thanks.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    ok

  • User Avatar
    0
    duongdangquoc@gmail.com created

    hi

    Can you move the ObjectExtensionManager.Instance.MapEfCoreProperty<IdentityUser, string> code to a common module and reference it in identity server and authserver projects?

    Hi maliming,

    In the AuthServer, it does not include the DbContextFactory so which file should I add ObjectExtensionManager.Instance.MapEfCoreProperty<IdentityUser, string>

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you try to add the same code(ObjectExtensionManager.Instance.MapEfCoreProperty<IdentityUser, string>) in identity and authserver projects?

    The code can be duplicated.

  • User Avatar
    0
    duongdangquoc@gmail.com created

    Hi maliming,

    In the IdentityService project, code should be added to IdentityServiceEfCoreEntityExtensionMappings.cs then this file will be referenced to IdentityServiceDbContextFactory : IDesignTimeDbContextFactory<IdentityServiceDbContext>

    But in the AuthServer project, where can I add the same code?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Call the IdentityServiceEfCoreEntityExtensionMappings.Configure(); in the Auth Sever module's PreConfigureServices

    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        IdentityServiceEfCoreEntityExtensionMappings.Configure();
    }
    
  • User Avatar
    0
    duongdangquoc@gmail.com created

    Hi maliming,

    Now I want to add those properties to ICurrentUser, how can I do, please

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    https://abp.io/docs/latest/framework/fundamentals/authorization#claims-principal-factory

Made with ❤️ on ABP v9.1.0-preview. Updated on December 02, 2024, 12:35