ASP身份 - 访问的HttpContext上的参考库 [英] ASP Identity - Accessing HttpContext on a reference library

查看:251
本文介绍了ASP身份 - 访问的HttpContext上的参考库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的ASP身份逻辑移动到另一个项目中我的解决方案中。

I am trying to move my ASP Identity logic to another project inside my solution.

一切都只是事实,我无法访问的HttpContext 我的参考图书馆内的罚款。我需要访问的HttpContext 使用种子我的数据库中的 ApplicationUserManager

Everything is fine except the fact that I cannot access HttpContext inside my reference library. I need to access HttpContext to seed my database using the ApplicationUserManager.

public class ApplicationDbInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        InitializeIdentityForEF(context);
        base.Seed(context);
    }

    public static void InitializeIdentityForEF(ApplicationDbContext db)
    {            
        var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();

        // Create here users and roles using userManager and roleManager
    }
}

所以,问题是:如何访问ApplicationUserManager和ApplicationRoleManager没有HttpContext的,不然我怎么可以访问这里的HttpContext

So the question is : How to access ApplicationUserManager and ApplicationRoleManager without HttpContext, or how can I access HttpContext here.

非常感谢

推荐答案

其实,我建议稍微不同的方法。身份团队,我们承诺什么,我认为有点弥天大罪的自​​举基于OWIN一个临时搭建的依赖注入容器中的样本。依赖注入是一个伟大的想法,但是不喜欢这样。可以理解的是不同的开发商preFER不同的DI容器,这使得基于一个真实的DI容器创建一个示例项目一个有点不方便,但他们应该从使用依赖注入示例项目忍住了,只是推荐使用依赖注入或许给出指导各种容器来实现这一点。

Actually, I would suggest a slightly different approach. The Identity team committed what I consider a bit of a mortal sin in bootstrapping the sample with a makeshift dependency injection container based on OWIN. Dependency injection is a great idea, but not like this. Understandably different developers prefer different DI containers, which makes creating a sample project based on a real DI container a bit of a no-go, but they should have refrained from utilizing dependency injection in the sample project and simply recommended the use of dependency injection and perhaps given guidance to implement this with various containers.

所有这一切说,唯一的HttpContext 被用于这里是获得OWIN背景下,这本身是被用来处理他们半生不熟的依赖注入对于身份。所以,如果你简单地注入自己的的UserManager 与您选择的DI容器,可以删除对的HttpContext 和你的依赖'重新好去。

All that said, the only thing HttpContext is being used for here is to get the OWIN context, and that alone is being used to handle their half-baked dependency injection for Identity. So, if you simply inject your own UserManager with your DI container of choice, you remove the dependency on HttpContext and you're good to go.

您只需要两件事提供的配置:的UserManager IUserStore 。告诉你的DI容器如何注入这些,然后你可以添加一个依赖于的UserManager 库中的类的构造函数,你就可以开始了。

You just need to provide configuration for two things: UserManager and IUserStore. Tell your DI container how to inject those, and then you can just add a dependency on UserManager in your library class constructor and you're ready to roll.

这篇关于ASP身份 - 访问的HttpContext上的参考库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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