Structuremap x Asp.net身份

时间:2014-11-03 03:43:30

标签: asp.net structuremap identity

我在注册像这样的依赖项时遇到了问题。

No default Instance is registered and cannot be automatically determined for type 'IUserStore<ApplicationIdentityUser, Int32>'

There is no configuration specified for IUserStore<ApplicationIdentityUser, Int32>


1.) new UserManager`2(*Default of IUserStore<ApplicationIdentityUser, Int32>*)
2.) UserManager<ApplicationIdentityUser, Int32>
3.) Instance of UserManager<ApplicationIdentityUser, Int32>
4.) new ApplicationUserManager(*Default of UserManager<ApplicationIdentityUser, Int32>*, *Default of IAuthenticationManager*)
5.) MyClinic.Infra.Data.Identity.ApplicationUserManager
6.) Instance of MyClinic.Core.Identity.IApplicationUserManager (MyClinic.Infra.Data.Identity.ApplicationUserManager)
7.) new AccountController(*Default of IApplicationUserManager*)
8.) MyClinic.Web.Controllers.AccountController
9.) Instance of MyClinic.Web.Controllers.AccountController
10.) Container.GetInstance(MyClinic.Web.Controllers.AccountController)

有人可以帮助我吗?

我的帐户管理员是:

public class AccountController : Controller
{
    private IApplicationUserManager _userManager;
    public AccountController(IApplicationUserManager userManager)
    {
        _userManager = userManager;
    }
}

我的应用程序用户管理器:

public class ApplicationUserManager : IApplicationUserManager
{
    private readonly UserManager<ApplicationIdentityUser, int> _userManager;
    private readonly IAuthenticationManager _authenticationManager;
    private bool _disposed;

    public ApplicationUserManager(UserManager<ApplicationIdentityUser, int> userManager, IAuthenticationManager authenticationManager)
    {
        _userManager = userManager;
        _authenticationManager = authenticationManager;
    }
}

2 个答案:

答案 0 :(得分:16)

您需要将以下内容添加到您的structuremap容器中以使用Asp.net Identity。

For<IUserStore<ApplicationUser>>().Use<UserStore<ApplicationUser>>();
For<DbContext>().Use(() => new ApplicationDbContext());
For<IAuthenticationManager>().Use(() => HttpContext.Current.GetOwinContext().Authentication);

此外,您还需要安装包

Microsoft.Owin.Host.SystemWeb

获取扩展方法GetOwinContext()出现在HttpContext.Current

答案 1 :(得分:0)

对我来说,我必须添加以下代码行:

cfg.AddRegistry(new AspNetIdentityRegistry());

到我的StructureMapConfig班上:

using Heroic.Web.IoC;
using MyCorprateCRM.Web.Identity;
using System.Web.Http;
using System.Web.Mvc;
using StructureMap;
using StructureMap.Graph;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(RoundlabCRM.Web.StructureMapConfig), "Configure")]
namespace RoundlabCRM.Web
{
    public static class StructureMapConfig
    {
        public static void Configure()
        {
            ObjectFactory.Configure(cfg =>
            {
                cfg.Scan(scan =>
                {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                });

                cfg.AddRegistry(new ControllerRegistry());
                cfg.AddRegistry(new MvcRegistry());
                cfg.AddRegistry(new ActionFilterRegistry(namespacePrefix: "RoundlabCRM.Web"));
                cfg.AddRegistry(new AspNetIdentityRegistry());

                //TODO: Add other registries and configure your container!
            });

            var resolver = new StructureMapDependencyResolver();
            DependencyResolver.SetResolver(resolver);
            GlobalConfiguration.Configuration.DependencyResolver = resolver;
        }
    }
}

AspNetIdentityRegistry在哪里:

using System.Data.Entity;
using System.Web;
using MyCorprateCRM.Model.Entities;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using StructureMap.Configuration.DSL;

namespace MyCorprateCRM.Web.Identity
{
    public class AspNetIdentityRegistry : Registry
    {
        public AspNetIdentityRegistry()
        {
            For<IUserStore<User>>().Use<UserStore<User>>();
            For<DbContext>().Use<AppDbContext>();
            For<IAuthenticationManager>().Use(ctx => ctx.GetInstance<HttpRequestBase>().GetOwinContext().Authentication);
        }
    }
}