0

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

这是我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>(); 
+0

你真的应该显示错误,但它因'signInManager'定义:'ApplicationSignInManager _signInManager'和'_signInManager = new SignInManager (signIn经理)'? – qujck

+0

现在它说:使用泛型类型'SignInManager '需要2个类型参数@qujck – Hatef

+0

相关:[AspNet身份和IoC容器注册](http://tech.trailmax.info/2014/09/aspnet-identity- and-ioc-container-registration /) – NightOwl888

回答

0

试试这个:

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

@Hatef你能接受这个答案吗,或者你需要另一个解决方案? – Backs