Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a UserManager outside of built in dependency injection system

This is using asp.net core with identity and entity framework core. I working on a saas application where I have a separate admin web app where you can add new tenants to the system. After a tenant is creates the app creates a default database (database per tenant set up) for the new tenant. I want to add a default user to this new database but I'm struggling with creating user manager outside of the dependency injection system.

The admin web app uses the usermanager that is created in the startup.cs (via the built in DI system) as the manager for the admin app. When I go to add the user to the new tenants database I am not using the DI system I just want to create a UserManager with the IdentityDbContext associated with the connection string for the new tenants database.

I'm am using this after a new tenant is created in the admin app:

public class TenantDbInitializer
{
    private Tenant tenant;
    private ApplicationDbContext context;

    private UserManager<ApplicationUser> userManager;

    public TenantDbInitializer(Tenant tenant)
    {
        this.tenant = tenant;
    }

    public void Init()
    {
        // tenant contains connection string
        context = new ApplicationDbContext(tenant); 

        var userStore = new UserStore<ApplicationUser>(context);

        userManager = new UserManager<ApplicationUser>(.........
    }
}

The UserManager construction parameters contain items that I can't find examples on what instances I should use. Some of the interfaces appear to have default implementations but I'm not sure if this is the way to proceed (or to pass null). The constructor is:

    public UserManager(IUserStore<TUser> store,
        IOptions<IdentityOptions> optionsAccessor,
        IPasswordHasher<TUser> passwordHasher,
        IEnumerable<IUserValidator<TUser>> userValidators,
        IEnumerable<IPasswordValidator<TUser>> passwordValidators,
        ILookupNormalizer keyNormalizer,
        IdentityErrorDescriber errors,
        IServiceProvider services,
        ILogger<UserManager<TUser>> logger)

Looking at the identity source it appears that I can pass null in for some of these parameters but I want to make sure I understand what is going on here so I don't do anything incorrectly.

The source for user manager is here: https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNetCore.Identity/UserManager.cs

Thanks, Brian

like image 802
Brian Avatar asked Jul 07 '16 19:07

Brian


People also ask

Does ASP.NET Core provide built in dependency injection?

ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. For more information specific to dependency injection within MVC controllers, see Dependency injection into controllers in ASP.NET Core.

How many ways we can do Dependency Injection in .NET core?

There are three types of DI: Construction Injection, Setter Injection, Interface based Injection. The Construction Injection type of DI accepts their dependency at the constructor level which means that when creating an object of the class, their dependency passes through the constructor of the class.

What is IdentityDbContext?

IdentityDbContext() Initializes a new instance of the IdentityDbContext class. IdentityDbContext(DbContextOptions) Initializes a new instance of IdentityDbContext.


1 Answers

The solution of the problem in this way

static IUserStore<ApplicationUser> _store = new UserStore<ApplicationUser>(new context()); 
static UserManager<ApplicationUser> _userManager = new UserManager<ApplicationUser>(_store, null, new PasswordHasher<ApplicationUser>(), null, null, null, null, null, null);
like image 68
borahan arslan Avatar answered Oct 21 '22 10:10

borahan arslan