Base solution for your next web application
Open Closed

Replace service #9576


User avatar
0
giuseppe.tofani created

Hi, I've extended UserClaimsPrincipalFactory and now I want to register this service inside my CoreModule initialize method.

How can I replace service registered by
.AddAbpUserClaimsPrincipalFactory<UserClaimsPrincipalFactory>() ?

Thanks. Giuseppe Tofani


3 Answer(s)
  • User Avatar
    0
    zony created
    Support Team

    Hi giuseppe.tofani, You only need to replace the UserClaimsPrincipalFactory in the IdentityRegistrar with your implementation class. E.g: CustomUserClaimsPrincipalFactory.cs

        public class CustomUserClaimsPrincipalFactory : AbpUserClaimsPrincipalFactory<User, Role>
        {
            public CustomUserClaimsPrincipalFactory(AbpUserManager<Role, User> userManager,
                AbpRoleManager<Role, User> roleManager,
                IOptions<IdentityOptions> optionsAccessor) : base(userManager,
                roleManager,
                optionsAccessor)
            {
            }
    
            public override Task<ClaimsPrincipal> CreateAsync(User user)
            {
                Console.WriteLine("Your code.");
                return base.CreateAsync(user);
            }
    
            protected override Task<ClaimsIdentity> GenerateClaimsAsync(User user)
            {
                Console.WriteLine("Your code.");
                return base.GenerateClaimsAsync(user);
            }
        }
    

    IdentityRegistrar.cs

        public static class IdentityRegistrar
        {
            public static IdentityBuilder Register(IServiceCollection services)
            {
                services.AddLogging();
    
                return services.AddAbpIdentity<Tenant, User, Role>(options =>
                    {
                        options.Tokens.ProviderMap[GoogleAuthenticatorProvider.Name] = new TokenProviderDescriptor(typeof(GoogleAuthenticatorProvider));
                    })
                    .AddAbpTenantManager<TenantManager>()
                    .AddAbpUserManager<UserManager>()
                    .AddAbpRoleManager<RoleManager>()
                    .AddAbpEditionManager<EditionManager>()
                    .AddAbpUserStore<UserStore>()
                    .AddAbpRoleStore<RoleStore>()
                    .AddAbpSignInManager<SignInManager>()
                    .AddAbpUserClaimsPrincipalFactory<CustomUserClaimsPrincipalFactory>() // it's here.
                    .AddAbpSecurityStampValidator<SecurityStampValidator>()
                    .AddPermissionChecker<PermissionChecker>()
                    .AddDefaultTokenProviders();
            }
        }
    
  • User Avatar
    0
    giuseppe.tofani created

    This is not a good solution for me because of circular dependency.

    I have aspnetzero projects:

    NodriveApp.Application NodriveApp.Application.Shared ... NodriveApp.Web.Core (include my project NodriveApp.NoApplication) ... and my projects (I prefer to not merge my code inside aspnetzero code):

    NodriveApp.NoApplication NodriveApp.NoApplication.Shared NodriveApp.NoCore NodriveApp.NoCore.Shared Good solution for me would be replace UserClaimsPrincipalFactory working inside NodriveApp.NoCore .NodriveAppNoCoreModule, for example, with some code like this below (but this doesn't works)

    public override void Initialize() { IocManager.RegisterAssemblyByConvention(typeof(NodriveAppNoCoreModule).GetAssembly());

        Configuration.ReplaceService(typeof(UserClaimsPrincipalFactory), () =>
        {
            IocManager.Register&lt;UserClaimsPrincipalFactory, NoUserClaimsPrincipalFactory&gt;();
        });
    }
    

    Is this possible ? Can I use this strategy to separate my code from aspnetzero code ?

    If I use your solution I have to include NodriveApp.NoCore inside NodriveApp.Core and this causes circular dependency.

    Many thanks. Giuseppe Tofani

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If you can access IServiceCollection in your project, you can try https://stackoverflow.com/a/43591075.

    Framework registers this class here