如何将 MVC 5 身份验证添加到 Unity IoC? [英] How to add MVC 5 authentication to Unity IoC?

查看:23
本文介绍了如何将 MVC 5 身份验证添加到 Unity 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
{

}

任何帮助将不胜感激!

推荐答案

因为你的控制器上有两个构造函数,unity 会选择参数列表较长的一个,后者.它需要注入 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 的构造函数.Unity 试图解析它,但找不到任何可以直接使用的构造函数或解析其参数.

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 身份验证添加到 Unity IoC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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