gpt4 book ai didi

asp.net-mvc - 如何使用 Ninject 注入(inject) Identity 类?

转载 作者:行者123 更新时间:2023-12-02 02:44:59 26 4
gpt4 key购买 nike

我尝试在类中使用 UserManager,但收到此错误:

Error activating IUserStore{ApplicationUser}
No matching bindings are available, and the type is not self-bindable.

我使用默认的 Startup.cs,它为每个请求设置一个实例:

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

我能够获取 ApplicationDbContext 实例,我相信该实例是由 Owin 注入(inject)的(这是真的吗?):

public class GenericRepository<T> : IGenericRepository<T> where T : class
{
private ApplicationDbContext context;

public GenericRepository(ApplicationDbContext context)
{
this.context = context;
}
}

但我不能对 UserManager 执行相同的操作(它会引发之前显示的错误):

public class AnunciosService : IAnunciosService
{
private IGenericRepository<Anuncio> _repo;
private ApplicationUserManager _userManager;

public AnunciosService(IRepositorioGenerico<Anuncio> repo, ApplicationUserManager userManager)
{
_repo = repo;
_userManager = userManager;
}
}

Controller 像这样使用 UserManager:

public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}

我正在使用 ninject 来注入(inject)其他类,但是如何注入(inject) UserManager 及其依赖项并避免像在我的 Controller 中那样使用它?

最佳答案

我是这样注入(inject)的

kernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>();
kernel.Bind<UserManager<ApplicationUser>>().ToSelf();

现在它正在正常工作。

关于asp.net-mvc - 如何使用 Ninject 注入(inject) Identity 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29089877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com