Last update:
October 5, 2022
Extending ASP.NET Identity UserManager and RoleManager
Virto Commerce has a custom implementation of default UserManager
and RoleManager
, which is VirtoCommerce.Platform.Web.Security.CustomUserManager and VirtoCommerce.Platform.Web.Security.CustomRoleManager, respectively.
Overriding UserManager with Custom Implementation¶
Virto's custom User Manager is registered in the DI as the UserManager
type and a factory method, which allows using UserManager within the scoped requests.
module.cs
public void Initialize(IServiceCollection serviceCollection)
{
...
serviceCollection.AddScoped<UserManager<ApplicationUser>, MyCustomUserManager>();
...
}
Custom RoleManager
is registered in the same way.
Usage¶
You can get both user and role managers by adding the respective factory to your service constructor:
public class MyCoolService
{
private readonly Func<UserManager<ApplicationUser>> _userManagerFactory;
private readonly Func<RoleManager<Role>> _roleManagerFactory;
public MyCoolService(Func<UserManager<ApplicationUser>> userManagerFactory, Func<RoleManager<Role>> roleManagerFactory)
{
_userManagerFactory = userManagerFactory;
_roleManagerFactory = roleManagerFactory;
}
public void DoMyCoolWork()
{
using var userManager = userManagerFactory();
using var roleManager = roleManagerFactory();
...
}
}
Note
In common cases, you do not need to get User or Role Manager directly by type. You can use factories instead.
References¶
Check out these articles for more information on the user and role management: