ASP.NET MVC身份默认实现 [英] ASP.NET MVC Identity Default Implementation

查看:108
本文介绍了ASP.NET MVC身份默认实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认的WEP应用在VS 2013年个人用户帐户附带有以下code帐户控制:

The default Wep Application in VS 2013 with "Individual User Accounts" comes with an account controller with the following code :

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

    private ApplicationUserManager _userManager;

    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

和此行Startup.Auth.cs

and this line in Startup.Auth.cs

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

我想明白了什么机制传递的UserManager参数构造函数。我相信dependendy喷射模式用在这里。
如果我是正确的,凡在Visual Studio解决方案我可以找到code负责依赖注入?

I'd like to understand what mechanism passes userManager parameter to the constructor. I believe dependendy injection pattern is used here. If I'm correct, where in the Visual Studio solution can I find the code responsible for dependency injection ?

接下来的部分的UserManager,我们为什么要测试是否_userManager为null,如果它在控制器中建立?

Next for the UserManager part, why should we test if _userManager is null if it's been set up in the controller ?

推荐答案

您发现媒体链接的code负责做的依赖注入。 app.CreatePerOwinContext()为code登记在Owin管道的UserManager(代表对创建的UserManager),这实际上只是被保存在一个字典在的HttpContext

You allready found the code responsible for doing the dependency injection. app.CreatePerOwinContext() is the code for registering the usermanager (a delegate to create the usermanager) in the Owin pipeline, which is actually just a dictionary which is saved in the HttpContext.

您可以深入阅读本机制在此<一个是如何工作的href=\"http://tech.trailmax.info/2014/09/owincontext-and-why-you-can-have-many-of-them/\">blogpost.

You can read in depth how this mechanism works in this blogpost.

至于您的其他问题,我完全理解!为什么检查null如果依赖注入......?好了:那是因为Owin在这里作为一个穷人的DI机制,并在默认项目模板看看究竟是怎样一个回退机制与的服务定位器反面模式。由于Owin使用一个体面的DI容器,而不是MVC pipeling需要一个默认的构造函数,因此检查为空和服务定位器是必要的。 MVC如何决定使用哪个构造是我不清楚。但我发现,有时候的UserManager注入得到,将在其他情况下的空。大概是因为owin背景是大多数只在创建账户控制器后可用的时间。

Regarding your other question which I completely understand! Why check for null if the dependency is injected...? Well: That's because Owin is used here as a poor man's DI mechanism and what you see in the default project template is actually a fallback mechanism with a smell of the service locator anti pattern. Because Owin is used instead of a decent DI container the MVC pipeling needs a default constructor, and therefore a check for null and the service locator is necessary. How MVC decides which constructor to use is not clear to me. But I found that the usermanager sometimes got injected and will be null in other scenario's. Probably because the owin context is most of the time only available after the creation of the Account controller.

虽然模板工程外的开箱它真的移动我的眉毛,就像它移动你的。于是我就用干净的实现体面DI容器中,并得到了去除大部分的owin服务定位器的东西。

While the template works out-of-the-box it really moves my eyebrows, just as it moves yours. So I went with a clean implementation of a decent DI container and got the remove most of the owin service locator stuff.

如果你有兴趣,你可以在这里找到我的解决方案 我曾经的Simple注射器的。而且还有其他DI容器解决方案这里

If your interested you can find my solution here where I used Simple Injector. And there are solutions for other DI containers here

这篇关于ASP.NET MVC身份默认实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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