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.

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

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?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    duongdangquoc@gmail.com created

    Yeah, let me try.

    Thanks.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    ok

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • 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>

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • 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.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • 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?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • 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();
    }
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    duongdangquoc@gmail.com created

    Hi maliming,

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

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

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

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 09, 2026, 11:56
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.