15

这是我的设置:

public class ApplicationUser : IdentityUser<Guid>
{
}
public class ApplicationRole : IdentityRole<Guid>
{
}
public class ApplicationUserLogin : IdentityUserLogin<Guid>
{
}
public class ApplicationUserClaim : IdentityUserClaim<Guid>
{
}
public class ApplicationRoleClaim : IdentityRoleClaim<Guid>
{
}

这是我的 UserStore 的定义

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, MyContext, Guid>
{
    public ApplicationUserStore(MyContext context, IdentityErrorDescriber describer = null)
        : base(context, describer)
    {
    }
}

这是我的 UserManager 的定义

public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor,
        IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators,
        IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer,
        IdentityErrorDescriber errors, IEnumerable<IUserTokenProvider<ApplicationUser>> tokenProviders,
        ILoggerFactory logger, IHttpContextAccessor contextAccessor)
        : base(
            store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors,
            tokenProviders, logger, contextAccessor)
    {
    }
}

这是我的 DbContext 的定义:

public class MyContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}

这是我的 Startup.cs

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<MyContext>(options => options.UseSqlServer(Configuration.Get("Data:DbConnection")));

        services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<MyContext, Guid>()
            .AddUserStore<ApplicationUserStore>()
            .AddRoleStore<ApplicationRoleStore>()
            .AddUserManager<ApplicationUserManager>()
            .AddRoleManager<ApplicationRoleManager>()
            .AddDefaultTokenProviders();

        var builder = new ContainerBuilder();
        builder.Populate(services);
        var container = builder.Build();
        return container.Resolve<IServiceProvider>();
    }

此构造函数的依赖项将起作用:

public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)

这个不会:

public AccountController(ApplicationUserManager userManager, SignInManager<ApplicationUser> signInManager)

任何人都知道我做错了什么?

4

3 回答 3

12

DI 通常用于接口驱动的开发;.AddUserManager<ApplicationUserManager>()指定实现UserManager<>,而不是服务接口。这意味着它仍然希望您以这种方式获得UserManager<ApplicationUser>并仅以这种方式使用它;它会给你一个ApplicationUserManager.

我假设您有其他方法要在ApplicationUserManager. 如果没有, 只需按照它的工作方式使用依赖构造函数,享受接口驱动的开发。如果是这样,您有 3 个选项:

  1. 通过组合而不是继承使用扩展。与其继承自UserManager<>,不如写成ApplicationUserManager一个包装类;您可以将其包含在构造函数中。这应该为您提供ApplicationUserManager.

  2. 自己将其按原样添加到 DI 框架中。这并不像听起来那么困难,因为UserManager<>本身没有真实状态:

    services.AddScoped<ApplicationUserManager>();
    

    这里的缺点是您实际上将有两个UserManager<>用于用户范围的对象;结果可能会导致一些效率低下。从当前代码的状态来看,我认为不是。

  3. 把它写成扩展方法。如果您有许多依赖项,而不仅仅是UserManager<>'s 的基本功能,那么这可能非常复杂。

于 2015-05-26T11:42:46.327 回答
5

我现在使用 ASP.NET Core 1.1,并且此行为已得到修复。

我可以轻松实现自己的 UserManager 和 UserStore,然后按以下方式引导应用程序:

// identity models
services
    .AddIdentity<ApplicationUser, ApplicationRole>()
    .AddEntityFrameworkStores<ApplicationDbContext, Guid>()
    .AddUserManager<ApplicationUserManager>()
    .AddUserStore<ApplicationUserStore>()
    .AddDefaultTokenProviders();

并将 UserManager 和 UserStore 都注入到我的控制器中,没有任何问题:

public AccountController(
    IIdentityServerInteractionService interaction,
    IClientStore clientStore,
    IHttpContextAccessor httpContextAccessor,
    ApplicationUserManager userManager,
    SignInManager<ApplicationUser> signInManager,
    IEmailSender emailSender,
    ISmsSender smsSender,
    ILoggerFactory loggerFactory)
{
    _interaction = interaction;
    _userManager = userManager;
    _signInManager = signInManager;
    _emailSender = emailSender;
    _smsSender = smsSender;
    _logger = loggerFactory.CreateLogger<AccountController>();
    _account = new AccountService(_interaction, httpContextAccessor, clientStore);
}
于 2017-03-16T14:27:24.753 回答
0

我想出了这个:

// Extract IApplicationUserManager interface with all methods you are using
public class ApplicationUserManager : UserManager<ApplicationUser>, IApplicationUserManager

// Register your custom user manager
services.AddIdentity<ApplicationUser, ApplicationRole>()
    .AddUserManager<ApplicationUserManager>();

// Return same scoped instance whenever you injecting your custom user manager into any constructor
services.AddScoped<IApplicationUserManager>(s => s.GetService<ApplicationUserManager>());
于 2020-02-06T09:28:29.697 回答