在内置依赖项注入系统之外创建UserManager [英] Creating a UserManager outside of built in dependency injection system

查看:110
本文介绍了在内置依赖项注入系统之外创建UserManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这使用具有身份和实体框架核心的asp.net核心.我在一个saas应用程序上工作,其中有一个单独的管理Web应用程序,您可以在其中向系统添加新的租户.创建租户后,应用程序将为新的租户创建一个默认数据库(每个租户设置的数据库).我想向该新数据库添加一个默认用户,但是我正在努力在依赖项注入系统之外创建用户管理器.

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.

管理Web应用程序使用在startup.cs中创建的用户管理器(通过内置的DI系统)作为管理应用程序的管理器.当我将用户添加到新的租户数据库中时,我没有使用DI系统,我只想创建一个IdentityManagerbnContext与新的租户数据库的连接字符串关联的UserManager.

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>(.........
    }
}

UserManager构造参数包含一些我找不到应使用的实例的示例.一些接口似乎具有默认实现,但是我不确定这是否是进行(或传递null)的方法.构造函数是:

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)

在查看身份来源时,我似乎可以为某些参数传递null,但是我想确保我了解这里发生的事情,所以我不会做任何错误的事情.

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.

用户管理器的来源在这里: https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNetCore.Identity/UserManager.cs

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

谢谢, 布莱恩

推荐答案

在浏览了身份源之后,您可以传递null以获得正确的结果.应该先尝试这个.

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

这篇关于在内置依赖项注入系统之外创建UserManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆