Identity Server 4 Asp.Net身份+ EF核心不播种

时间:2019-04-01 10:02:21

标签: c# asp.net-core identityserver4

除了跳过javascript客户端之外,我基本上从头开始遵循整个教程。但是,我从示例Asp.Net Identity + EF Core组合中复制了一些代码,并成功创建了所有表的数据库。但是,当我运行身份服务器时,没有种子。我调试了program.cs,种子var始终为假,从而跳过了种子条件。

public class Program
{
    public static void Main(string[] args)
    {
        var seed = args.Any(x => x == "/seed");
        if (seed) args = args.Except(new[] { "/seed" }).ToArray();

        var host = CreateWebHostBuilder(args).Build();

        if (seed)
        {
            using (var scope = host.Services.GetRequiredService<IServiceScopeFactory>().CreateScope())
            {
                SeedData.EnsureSeedData(scope.ServiceProvider);
                return;
            }
        }

        host.Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

这是样本数据中的种子数据类

 public class SeedData
{
    public static void EnsureSeedData(IServiceProvider provider)
    {
        provider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
        provider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
        provider.GetRequiredService<ConfigurationDbContext>().Database.Migrate();

        {
            var userMgr = provider.GetRequiredService<UserManager<ApplicationUser>>();
            var alice = userMgr.FindByNameAsync("alice").Result;
            if (alice== null)
            {
                alice = new ApplicationUser
                {
                    UserName = "alice"
                };
                var result = userMgr.CreateAsync(alice, "Pass123$").Result
                if (!result.Succeeded)
                {
                    throw new Exception(result.Errors.First().Description);
                }

                alice = userMgr.FindByNameAsync("alice").Result;

                result = userMgr.AddClaimsAsync(user, new Claim[]{
                            new Claim(JwtClaimTypes.Subject, "1"),
                            new Claim(JwtClaimTypes.Name, "Alice Smith"),
                            new Claim(JwtClaimTypes.GivenName, "Alice"),
                            new Claim(JwtClaimTypes.FamilyName, "Smith"),
                              new Claim(JwtClaimTypes.Email, "AliceSmith@email.com"),
                            new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
                            new Claim(JwtClaimTypes.Role, "Admin")                                
                        }).Result;
                if (!result.Succeeded)
                {
                    throw new Exception(result.Errors.First().Description);
                }
                Console.WriteLine("user created");
            }
            else
            {
                Console.WriteLine("user already exists");
            }                
        }

        {
            var context = provider.GetRequiredService<ConfigurationDbContext>();
            if (!context.Clients.Any())
            {
                foreach (var client in Config.GetClients())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }

            if (!context.IdentityResources.Any())
            {
                foreach (var resource in Config.GetIdentityResources())
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }

            if (!context.ApiResources.Any())
            {
                foreach (var resource in Config.GetApis())
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
        }
    }
}

我想念什么?所有明智的代码都是相同的,启动类也是如此...

1 个答案:

答案 0 :(得分:0)

要传递string[] args,您需要通过Project.exe而不是IIS Express启动项目。

遵循项目属性->调试->应用程序参数-> /seed->从ProjectName启动项目。

通常,您可以考虑通过检查数据库中是否有任何数据而不是来自命令参数来确定种子数据。

相关问题