更新IdentityServer4的客户端数据库

时间:2018-04-17 21:50:05

标签: identityserver4

我的netnet应用程序中有identityServer4作为我的oAuth服务器。我能够使用以下代码初始化我的客户端数据库:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
            {
                SeedData.EnsureSeedData(serviceScope);
            }
        } 
    }

使用

完成迁移和客户端初始化
  public class SeedData
{
    public static void EnsureSeedData(IServiceScope serviceScope)
    {
        Console.WriteLine("Seeding database...");

           serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();

            var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
            context.Database.Migrate();

            EnsureSeedData(context);

        Console.WriteLine("Done seeding database.");
        Console.WriteLine();
    }

    private static void EnsureSeedData(ConfigurationDbContext context)
    {
        if (!context.Clients.Any())
        {
            Console.WriteLine("Clients being populated");
            foreach (var client in Config.GetClients().ToList())
            {
                context.Clients.Add(client.ToEntity());
            }
            context.SaveChanges();
        }
        else
        {
           Console.WriteLine("Clients already populated, update clients");
                foreach (var client in Config.GetClients().ToList())
                {
                    var item = context.Clients.Where(c => c.ClientId == client.ClientId).FirstOrDefault();
                   if(item == null)
                    {
                        context.Clients.Add(client.ToEntity());
                    } else {
                        var model = client.ToEntity();
                        model.Id = item.Id;
                        context.Entry(item).CurrentValues.SetValues(model);
                    }                       
                }
                context.SaveChanges();
            }

            if (!context.IdentityResources.Any())
        {
            Console.WriteLine("IdentityResources being populated");
            foreach (var resource in Config.GetIdentityResources().ToList())
            {
                context.IdentityResources.Add(resource.ToEntity());
            }
            context.SaveChanges();
        }
        else
        {
           Console.WriteLine("IdentityResources already populated");
                foreach (var resource in Config.GetIdentityResources().ToList())
                {
                    var item = context.IdentityResources.Where(c => c.Name == resource.Name).FirstOrDefault();
                    if (item == null)
                    {
                        context.IdentityResources.Add(resource.ToEntity());
                    }
                    else
                    {
                        var model = resource.ToEntity();
                        model.Id = item.Id;
                        context.Entry(item).CurrentValues.SetValues(model);
                    }
                }
                context.SaveChanges();
            }

            if (!context.ApiResources.Any())
        {
            Console.WriteLine("ApiResources being populated");
            foreach (var resource in Config.GetApiResources().ToList())
            {
                context.ApiResources.Add(resource.ToEntity());
            }
            context.SaveChanges();
        }
        else
        {
           Console.WriteLine("ApiResources already populated");
                foreach (var resource in Config.GetApiResources().ToList())
                {
                    var item = context.ApiResources.Where(c => c.Name == resource.Name).FirstOrDefault();
                    if (item == null)
                    {
                        context.ApiResources.Add(resource.ToEntity());
                    }
                    else
                    {
                    var model = resource.ToEntity();
                    model.Id = item.Id;
                    context.Entry(item).CurrentValues.SetValues(model);
                    }
                }
               context.SaveChanges();
            }
    }
}

客户端数据库的初始化效果很好。但是,更新客户端数据库存在问题。某些记录未更新。有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

经过一番努力,我找到了一个适合我的解决方案。基本上,我创建了一个跟踪客户端定义更改的表ClientVersion。

这是我的新版SeedData.cs。

   public class SeedData
{
    public static void EnsureSeedData(IServiceScope serviceScope)
    {
        Console.WriteLine("Seeding database...");

        serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();

        var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
        context.Database.Migrate();

        var versionContext = serviceScope.ServiceProvider.GetRequiredService<VersionContext>();
        versionContext.Database.Migrate();

        EnsureSeedData(context, versionContext);

        Console.WriteLine("Done seeding database.");
        Console.WriteLine();
    }

    private static void EnsureSeedData(ConfigurationDbContext context, VersionContext versionContext)
    {
        ClientVersion version = versionContext.ClientVersion.FirstOrDefault();
        bool blUpdate = version == null ? true : (Config.CurrentVersion > version.Version ? true: false);
        if(blUpdate)
        {
            if(version == null)
            {
                version = new ClientVersion()
                {
                    Version = Config.CurrentVersion,
                };
                versionContext.ClientVersion.Add(version);
            } else
            {
                version.Version = Config.CurrentVersion;
                versionContext.ClientVersion.Update(version);
            }
            versionContext.SaveChanges();
        }
        if (!context.Clients.Any())
        {
            Console.WriteLine("Clients being populated");
            foreach (var client in Config.GetClients().ToList())
            {
                context.Clients.Add(client.ToEntity());
            }
            context.SaveChanges();
        }
        else
        {
            Console.WriteLine("Clients already populated, update clients");
            if (blUpdate)
            {
                foreach (var client in Config.GetClients().ToList())
                {
                    var item = context.Clients
                        .Include(x => x.RedirectUris)
                        .Include(x => x.PostLogoutRedirectUris)
                        .Include(x => x.ClientSecrets)
                        .Include(x => x.Claims)
                        .Include(x => x.AllowedScopes)
                        .Include(x => x.AllowedCorsOrigins)
                        .Include(x => x.AllowedGrantTypes)
                        .Where(c => c.ClientId == client.ClientId).FirstOrDefault();
                    if (item != null)
                    {
                        context.Clients.Remove(item);
                    }
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }
        }

        if (!context.IdentityResources.Any())
        {
            Console.WriteLine("IdentityResources being populated");
            foreach (var resource in Config.GetIdentityResources().ToList())
            {
                context.IdentityResources.Add(resource.ToEntity());
            }
            context.SaveChanges();
        }
        else
        {
            Console.WriteLine("IdentityResources already populated");
            if (blUpdate)
            {
                foreach (var resource in Config.GetIdentityResources().ToList())
                {
                    var item = context.IdentityResources.Where(c => c.Name == resource.Name).FirstOrDefault();
                    if (item != null)
                    {
                        context.IdentityResources.Remove(item);
                    }
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
        }

        if (!context.ApiResources.Any())
        {
            Console.WriteLine("ApiResources being populated");
            foreach (var resource in Config.GetApiResources().ToList())
            {
                context.ApiResources.Add(resource.ToEntity());
            }
            context.SaveChanges();
        }
        else
        {
            Console.WriteLine("ApiResources already populated");
            if (blUpdate)
            {
                foreach (var resource in Config.GetApiResources().ToList())
                {
                    var item = context.ApiResources.Where(c => c.Name == resource.Name).FirstOrDefault();
                    if (item != null)
                    {
                        context.ApiResources.Remove(item);
                    }
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
        }
    }
}