加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 23:07:28 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试在类中使用UserManager,但我收到此错误: Error activating IUserStore{ApplicationUser}No matching bindings are available,and the type is not self-bindable. 我正在使用默认的Startup.cs,它为每个请求设置一个实例: app.CreatePerOwinConte
我正在尝试在类中使用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();

而现在它正在发挥作用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读