- 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)
-
0
hi
Can you move the
ObjectExtensionManager.Instance.MapEfCoreProperty<IdentityUser, string>
code to a common module and reference it in identity server and authserver projects? -
0
Yeah, let me try.
Thanks.
-
0
ok
-
0
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>
-
0
hi
Can you try to add the same code(
ObjectExtensionManager.Instance.MapEfCoreProperty<IdentityUser, string>
) in identity and authserver projects?The code can be duplicated.
-
0
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?
-
0
hi
Call the
IdentityServiceEfCoreEntityExtensionMappings.Configure();
in the Auth Sever module'sPreConfigureServices
public override void PreConfigureServices(ServiceConfigurationContext context) { IdentityServiceEfCoreEntityExtensionMappings.Configure(); }
-
0
Hi maliming,
Now I want to add those properties to ICurrentUser, how can I do, please
-
0
hi
https://abp.io/docs/latest/framework/fundamentals/authorization#claims-principal-factory