private void InitializeIdentityServerDatabase(IApplicationBuilder app)
 {
     using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
     {
         serviceScope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate();
         var context = serviceScope.ServiceProvider.GetRequiredService <ConfigurationDbContext>();
         context.Database.Migrate();
         if (!context.Clients.Any())
         {
             foreach (IdentityServer4.Models.Client client in IS4Config.GetClients())
             {
                 context.Clients.Add(client.ToEntity());
             }
             context.SaveChanges();
         }
         if (!context.IdentityResources.Any())
         {
             foreach (IdentityServer4.Models.IdentityResource resource in IS4Config.IdentityResources())
             {
                 context.IdentityResources.Add(resource.ToEntity());
             }
             context.SaveChanges();
         }
         if (!context.ApiResources.Any())
         {
             foreach (var resource in IS4Config.GetAllApiResources())
             {
                 context.ApiResources.Add(resource.ToEntity());
             }
             context.SaveChanges();
         }
     }
 }
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("appsettings.json", false)
                         .Build();
            //
            var migrationAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            //.AddInMemoryIdentityResources(IS4Config.IdentityResources())
            //.AddInMemoryApiResources(IS4Config.GetAllApiResources())
            //.AddInMemoryClients(IS4Config.GetClients())
            //Configuration store: clients & resources.
            .AddTestUsers(IS4Config.GetUsers())
            .AddConfigurationStore(options => {
                options.ConfigureDbContext = b =>
                                             b.UseSqlServer(config.GetConnectionString("AppData"), sql => sql.MigrationsAssembly(migrationAssembly));
            })
            //Operational Store: Tokens, Consents, Codes, etc....
            .AddOperationalStore(options => {
                options.ConfigureDbContext = b =>
                                             b.UseSqlServer(config.GetConnectionString("AppData"), sql => sql.MigrationsAssembly(migrationAssembly));
            });



            //services.AddMvc();
            services.AddControllersWithViews();
        }