如何在 EF Core 2.1.0 中为管理员用户做种? [英] How to seed an Admin user in EF Core 2.1.0?

查看:19
本文介绍了如何在 EF Core 2.1.0 中为管理员用户做种?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 EF Core 2.1.0 的 ASP.NET Core 2.1.0 应用程序.

I have an ASP.NET Core 2.1.0 application using EF Core 2.1.0.

我如何用管理员用户植入数据库并给他/她一个管理员角色?我找不到任何关于此的文档.

How do I go about seeding the database with Admin user and give him/her an Admin role? I cannot find any documentation on this.

推荐答案

由于用户无法在 Identity 中以正常方式播种,就像使用 .HasData() 的其他表播种一样.NET 核心 2.1.

As user cannot be seeded in a normal way in Identity just like other tables are seeded using .HasData() of .NET Core 2.1.

Microsoft 建议:对于需要调用外部 API 的数据,例如 ASP.NET Core Identity 用户创建,建议使用自定义初始化逻辑.

Microsoft Recommendation: For data that requires calls to external API, such as ASP.NET Core Identity users creation it is recommended to use custom initialization logic.

Seed Roles 使用以下 ApplicationDbContext 类中给出的代码:

Seed Roles in .NET Core 2.1 using code given below in ApplicationDbContext Class :

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

        modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "Admin", NormalizedName = "Admin".ToUpper() });
    }

按照以下步骤为用户播种.

第 1 步:创建新类

public static class ApplicationDbInitializer
{
    public static void SeedUsers(UserManager<IdentityUser> userManager)
    {
        if (userManager.FindByEmailAsync("abc@xyz.com").Result==null)
        {
            IdentityUser user = new IdentityUser
            {
                UserName = "abc@xyz.com",
                Email = "abc@xyz.com"
            };

            IdentityResult result = userManager.CreateAsync(user, "PasswordHere").Result;

            if (result.Succeeded)
            {
                userManager.AddToRoleAsync(user, "Admin").Wait();
            }
        }       
    }   
}

第 2 步:现在修改 Startup.cs 类中的 ConfigureServices 方法.

Step 2: Now Modify ConfigureServices method in Startup.cs class.

修改前:

services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

修改后:

services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

第三步:修改Startup.cs类中Configure方法的参数.

修改前:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //..........
    }

修改后:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, UserManager<IdentityUser> userManager)
    {
        //..........
    }

第 4 步:调用我们的 Seed (ApplicationDbInitializer) 类的方法:

Step 4 : Calling method of our Seed (ApplicationDbInitializer) class:

ApplicationDbInitializer.SeedUsers(userManager);

注意:您还可以像用户一样种子角色,方法是将 RoleManagerUserManager 一起注入.

Note: You can also Seed Roles just like users by Injecting the RoleManager along with UserManager.

这篇关于如何在 EF Core 2.1.0 中为管理员用户做种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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