相关疑难解决方法(0)

如何将MVC 5身份验证添加到Unity IoC?

我正在努力在我的应用程序中实现新的ASP.NET MVC 5开箱即用身份验证.但是当使用Unity作为我的IoC时,我不能使用AccountController的任何部分,因为我给出了错误:

类型IUserStore`1没有可访问的构造函数.

这是我给出的统一设置,在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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是新的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) …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity

22
推荐指数
2
解决办法
7058
查看次数

asp.net identity 2.0统一不解析默认用户存储

尝试使用Unity.Mvc5和使用Identity 2.0和Identity 2.0 Samples样板的MVC 5应用程序配置Unity时,我得到以下异常.我已经阅读了这个SO 配置Unity DI的ASP.NET身份,我仍然不清楚我错过了什么.我在这做错了什么?

当前类型System.Data.Common.DbConnection是一个抽象类,无法构造.你错过了类型映射吗?

[ResolutionFailedException:依赖项的解析失败,type ="myApp.Web.Controllers.AccountController",name ="(none)".在解决时发生异常:

异常是:InvalidOperationException - 当前类型System.Data.Common.DbConnection是一个抽象类,无法构造.你错过了类型映射吗?

在例外时,容器是:

解析myApp.Web.Controllers.AccountController,(无)解析构造函数myApp.Web.Controllers.AccountController(myApp.Web.Models.ApplicationUserManager userManager)的参数"userManager"解析myApp.Web.Models.ApplicationUserManager,(无)解析参数构造函数myApp.Web.Models.ApplicationUserManager的"store"(Microsoft.AspNet.Identity.IUserStore 1[[myApp.Web.DAL.Profiles.ApplicationUser, myApp.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] store) Resolving Microsoft.AspNet.Identity.EntityFramework.UserStore1 [myApp.Web.DAL.Profiles.ApplicationUser],(无)(从Microsoft.AspNet.Identity.IUserStore 1[myApp.Web.DAL.Profiles.ApplicationUser], (none)) Resolving parameter "context" of constructor Microsoft.AspNet.Identity.EntityFramework.UserStore1 [[myApp ]映射).Web.DAL.Profiles.ApplicationUser,myApp.Web,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]](System.Data.Entity.DbContext context)Resolving System.Data.Entity.DbContext,(none)解析构造函数System.Data.Entity.DbContext的参数"existingConnection"(System.Data.Common.DbConnection existingConnection,System.Data.Entity.Infrastructure.DbCompiledModel model,System.Boolean contextOwnsConnection)Resolving System.Data.Common.DbConnection,(没有)

帐户控制器,因为我已修改它

 public AccountController(ApplicationUserManager userManager)
 {
      _userManager = userManager;
 }

 private ApplicationUserManager _userManager;
Run Code Online (Sandbox Code Playgroud)

我已注册的容器

container.RegisterType<ApplicationUserManager>(new HierarchicalLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc dependency-injection unity-container asp.net-identity asp.net-identity-2

10
推荐指数
1
解决办法
1万
查看次数

如何在C#中注入一个类(不是接口)?

我在这里使用Unity.但可能我们只需指向一个正确的方向.

我们知道如何注入接口:

public class AccountController:ApiController
{
    private readonly IAccountRepository _repository;

    public AccountController(IAccountRepository repository)
    {
        _repository = repository;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用RegisterType

var container = new UnityContainer();
container.RegisterType<IAccountRepository, AccountRepository>(new HierarchicalLifetimeManager());
Run Code Online (Sandbox Code Playgroud)

但是在我的AccountRepository中,我们将一个类注入到构造函数中.

private readonly ApplicationUserManager _userManager;
public AccountRepository(ApplicationUserManager userManager)
{
    _userManager = userManager;
}
Run Code Online (Sandbox Code Playgroud)

因此,在调用ApiController时,我仍然会收到此错误:

尝试创建"AccountController"类型的控制器时发生错误.确保控制器具有无参数的公共构造函数.

堆栈跟踪:

System.Web.Http.Detpatcher.DefaultHttpControllerActivator.GetInstanceOrActivator上的System.Web.Http.Internal.TypeActivator.Create [TBase](Type instanceType)中的System.Linq.Expressions.Expression.New(Type type)(HttpRequestMessage请求,类型System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,类型controllerType)中的controllerType,Func`1和activator)

由于我已经创建了一些其他工作正常的ApiControllers,我想这一定是因为我们的ApplicationUserManager无法解析.

这里ApplicationUserManager继承自UserManager而不是接口.我不能用container.RegisterType<interface, derived_class>.解决问题的正确方法是什么?


这是ApplicationUserManager:

public class ApplicationUserManager : UserManager<User>
{
    public ApplicationUserManager(IdentityContext identityContext)
        : base(new UserStore<User>(identityContext)) …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection unity-container

7
推荐指数
1
解决办法
5018
查看次数

当前类型IUserStore <ApplicationUser>和DbConnection分别是接口和抽象类,不能构造

如果我冲浪,http://localhost:58472/Account/Register我就有这个例外

System.InvalidOperationException:当前类型IUserStore<ApplicationUser>是一个接口,无法构造.你错过了类型映射吗?

这是我的代码:

public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    { }

    public static ApplicationUserManager Create(
        IdentityFactoryOptions<ApplicationUserManager> options, 
        IOwinContext context) 
    {
        var manager = new ApplicationUserManager(
            new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())
        );

        // other code
        return manager;   
    }
}
Run Code Online (Sandbox Code Playgroud)

这里是控制器的代码:

[Authorize]
public class AccountController : Controller
{
    private ApplicationSignInManager _signInManager;
    private ApplicationUserManager _userManager;

    public AccountController()
    {
    }

    public AccountController(
        ApplicationUserManager userManager,
        ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }

    public ApplicationSignInManager SignInManager …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc user-management unity-container invalidoperationexception

5
推荐指数
1
解决办法
6062
查看次数