ASP.NET身份和统一

时间:2016-07-06 10:37:09

标签: asp.net-mvc dependency-injection inversion-of-control unity-container ioc-container

我在项目中使用ASP.NET Identity和Unity来实现DI。 signInManager有问题。我不能在构造函数中使用它,因为它有错误。请帮我。 UserManager和RoleManager正常工作。但是如何使用signInManager作为接口。

这是我的AuthersController:

    private readonly UserManager<ApplicationUser> _userManager;
    private readonly RoleManager<IdentityRole> _roleManager;
    private readonly ApplicationSignInManager _signInManager;

    public AuthorsController(IUserStore<ApplicationUser> userManager, IRoleStore<IdentityRole, string> roleManager, IAuthenticationManager signInManager)
    {
        _userManager = new UserManager<ApplicationUser>(userManager);
        _roleManager = new RoleManager<IdentityRole>(roleManager);
        _signInManager = new SignInManager<ApplicationSignInManager>(signInManager);
    }

    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set
        {
            _signInManager = value;
        }
    }

这是我的UnityConfig:

        var Constructor = new InjectionConstructor(new ApplicationDbContext());
        container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(Constructor);
        container.RegisterType<IRoleStore<IdentityRole, string>, RoleStore<IdentityRole, string, IdentityUserRole>>(Constructor);

        // TODO: Register your types here
        container.RegisterType<IPostsRepository, PostsService>();
        container.RegisterType<ICategoriesRepository, CategoriesService>();
        container.RegisterType<ITagsRepository, TagsService>();

1 个答案:

答案 0 :(得分:0)

试试这个:

Container.RegisterType<IAuthenticationManager>(new InjectionFactory(c => HttpContext.Current.GetOwinContext().Authentication));