如何MVC 5认证添加到统一的IoC? [英] How to add MVC 5 authentication to Unity IoC?

查看:145
本文介绍了如何MVC 5认证添加到统一的IoC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实施的新的ASP.NET MVC 5外的箱子验证了我的申请。但是使用Unity作为我的IoC的时候,我无法使用的AccountController的任何部分,因为我给出的错误:

I'm currently working on implementing the new ASP.NET MVC 5 out-of-the box authentication into my application. However when using Unity as my IoC, I cannot use any portion of the AccountController because I'm given the error:

该型IUserStore`1不具有可访问的构造函数。

The type IUserStore`1 does not have an accessible constructor.

这是我给统一安装被称为在Global.asax

This is my given unity setup which is called in the global.asax

public class DependencyConfig
{
    public static void Initialise()
    {
        var container = BuildUnityContainer();

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }

    private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        container.RegisterType<IEmployeeRepository, EmployeeRepository>();

        container.RegisterType<ITeamRepository, TeamRepository>();

        container.RegisterType<ICompanyRepository, CompanyRepository>();

        return container;
    }
}

和这里有新鲜AccountController.cs的默认构造

And here are the default constructors of a fresh AccountController.cs

public AccountController()
        : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new BusinessTrackerUsersContext())))
{

}

public AccountController(UserManager<ApplicationUser> userManager)
{
    UserManager = userManager;
}

public AccountController(UserManager<ApplicationUser> userManager)
{
    UserManager = userManager;
}

在这里是被称为中的AccountController构造的项目。这些都是新名称的默认值。

And here are the items being called in the AccountController constructors. These are the defaults with new names.

public class BusinessTrackerUsersContext : IdentityDbContext<ApplicationUser>
{
    public BusinessTrackerUsersContext()
        : base("DefaultConnection")
    {

    }
}

public class ApplicationUser : IdentityUser
{

}

任何帮助将是广泛的AP preciated!

Any help would be widely appreciated!

推荐答案

由于你有你的控制器上有两个构造函数,统一将拿起一个较长参数列表,后者。它需要被注入所述的UserManager

Because you have two constructors on your controller, unity will pick up the one with longer parameter list, the latter. It requires the UserManager to be injected.

虽然你还没有列在这里吧,我怀疑是有需要的IUserStore唯一的构造。团结试图解决它,无法找到它可以直接使用或解决其参数的任何构造函数。

Although you haven't listed it here, I suspect is has an only constructor that requires the IUserStore. Unity tried to resolve it and can't find any constructor it could use directly or resolve its parameters.

这篇关于如何MVC 5认证添加到统一的IoC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆