asp.net-mvc – 如何使用Ninject注入Identity类?

我正在尝试在类中使用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注入的(这是真的吗?):

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

控制器像这样使用UserManager:

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

我正在使用ninject注入我的其他类,但是如何使用它的依赖项注入UserManager并避免在我的控制器中使用它?

解决方法

我像这样注射它
kernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>();
kernel.Bind<UserManager<ApplicationUser>>().ToSelf();

而现在它正在发挥作用.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....