移动标识2.0功能repository类 [英] Moving Identity 2.0 functions to repository class

查看:175
本文介绍了移动标识2.0功能repository类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的身份2.0我的申请,并希望移动数据功能,以库层,如下面的code:

I am using identity 2.0 for my application and want to move the data functionalities to repository layer such as the following code:

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

    //Create User=Admin@Admin.com with password=Admin@123456 in the Admin role        
    public static void InitializeIdentityForEF(ApplicationDbContext db) {
        var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();

现在的问题是的HttpContext 不生活在库层,你必须把它传递到该层。但即使你这样做,调用应该从网​​站层的到来。但你不希望包括的UserManager 在每次调用其他层。任何解决方案?

now the problem is HttpContext doesn't live in the repository layer and you have to pass it to that layer. but even if you do that, the call should be coming from the Web layer. yet you don't want to include userManager in every call to other layers. any solution?

推荐答案

我发现创建存储库层的用户管理方式:

i found the way to create the user manager on the repository layer:

            var roleStore = new RoleStore<IdentityRole>(context);
            var roleManager = new RoleManager<IdentityRole>(roleStore);
            var userStore = new UserStore<ApplicationUser>(context);
            var userManager = new UserManager<ApplicationUser>(userStore);               
            var user = new ApplicationUser { UserName = "sallen" };

            userManager.Create(user, "password");                    
            roleManager.Create(new IdentityRole { Name = "admin" });
            userManager.AddToRole(user.Id, "admin");

这篇关于移动标识2.0功能repository类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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