如何插入我的Autofac容器放入ASP。 NET 2.1身份 [英] How to plug my Autofac container into ASP. NET Identity 2.1

查看:120
本文介绍了如何插入我的Autofac容器放入ASP。 NET 2.1身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找到的ASP.NET 2.1身份及其配件的一个新版本的新功能集成到OWIN中间件新的IoC功能。
一说我在看的例子的句子是这样的:

I have been looking into the new features of the new version of ASP.NET Identity 2.1 and one of its enhancements is the new IoC features integrated into the OWIN Middleware. One of the sentences that I looked in the examples is this one:

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

这句话接收功能的委托返回提供的例子经理实施的新实例:

This sentence receives a function delegate which returns a new instance of a manager implementation provided on the examples:

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

我个人不喜欢这种实现,因为我不能够使用的容器注入,我想为这些经理的任何相关性。

I personally dont like this implementation because I am not able to use a container to inject any dependency that I want for these managers.

另外还有一个IdentityFactoryOptions和IOwinContext被神奇地注入到功能,我不是能够拉伸到我的IoC容器。

Also there is an "IdentityFactoryOptions" and a "IOwinContext" that are "magically" injected to the function which Im not able to pull out into my IoC container.

不要任何人有此实现一个更好的解决办法?

Do anyone have a better workaround on this implementation?

推荐答案

我是从外现成的MVC5安装启动和使用AutoFac作为IoC容器。这听起来像我想acheive类似的目标是你,所以让我解释我做了什么。作为一个声明,我是相当新的使用的IoC和身份。

I'm starting from an out-of-the-box MVC5 installation and using AutoFac as an IoC container. It sounds like I am trying to acheive a similar goal as you, so let me explain what I've done. As a disclaimer, I am fairly new to using IoC and to Identity.

我相信IOwinContext是作为国际奥委会的作用不必要的,如果你使用的是自己 - 我切换到注册我的AutoFac ApplicationUserManager。要做到这一点,我不得不

I believe the IOwinContext is unnecessary in a role as an IoC if you are using your own - I switched over to registering my ApplicationUserManager with AutoFac. To achieve this I had to:

从Startup.Auth删除CreatePerOwinContext行,因为我会注册 ApplicationDbContext ApplicationUserManager 在AutoFac。

Remove CreatePerOwinContext lines from Startup.Auth since I'll register ApplicationDbContext and ApplicationUserManager in AutoFac.

//app.CreatePerOwinContext(ApplicationDbContext.Create);
//app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

修改ApplicationUserManager构造函数的参数,其中包括一切从创建功能。

Modify the ApplicationUserManager constructor arguments and included everything from the Create function.

public ApplicationUserManager(IUserStore<ApplicationUser> store, IdentityFactoryOptions<ApplicationUserManager> options)
        : base(store)
{
    //all the code from the 'Create' function here, using `this` for `manager`
}

设置的AccountController有一个构造函数服用 ApplicationUserManager 作为参数,报废,抓住了的UserManager 属性在 ApplicationUserManager OwinContext

Set the AccountController to have a single constructor taking an ApplicationUserManager as an argument and scrapped the UserManager property that grabs the ApplicationUserManager from the OwinContext.

private ApplicationUserManager _userManager; //every thing that needs the old UserManager property references this now
public AccountController(ApplicationUserManager userManager) 
{
    _userManager = userManager;
}

注册与AutoFac的一切,包括IdentityFactoryOptions的一个实例。

Register everything with AutoFac, including an instance of IdentityFactoryOptions.

var x = new ApplicationDbContext();
builder.Register<ApplicationDbContext>(c => x);
builder.Register<UserStore<ApplicationUser>>(c => new UserStore<ApplicationUser>(x)).AsImplementedInterfaces();
builder.Register<IdentityFactoryOptions<ApplicationUserManager>>(c => new IdentityFactoryOptions<ApplicationUserManager>()
{
    DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ApplicationName")
});
builder.RegisterType<ApplicationUserManager>();

这是粗糙的总结。我可能已经错过了几个其他的扭动,我不得不一路上做的。

That's the rough summary. I may have missed a couple of other tweaks I had to do along the way.

这篇关于如何插入我的Autofac容器放入ASP。 NET 2.1身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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