Brian
Brian

Reputation: 2418

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

Upvotes: 14

Views: 6852

Answers (3)

borahan arslan
borahan arslan

Reputation: 81

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);

Upvotes: 4

Phil Huhn
Phil Huhn

Reputation: 4101

I would think you should have at least the UserStore:

 UserStore<ApplicationUser> _userStore = new UserStore<ApplicationUser>(context, null);
 Mock<ILogger<UserManager<ApplicationUser>>> mockLogger = null;
 mockLogger = new Mock<ILogger<UserManager<ApplicationUser>>>();

I mocked the logger.

Upvotes: 0

Brian
Brian

Reputation: 2418

After looking through the identity source you can pass nulls in to get the correct results. Should have just tried this first.

Upvotes: 7

Related Questions