/// <summary>
        /// IdentityServer em memória(clients, resources) e apenas IdentityUser no banco. Seleciona entre SQLlite e SQLServer, default SQLLite.
        /// </summary>
        /// <param name="services"></param>
        /// <param name="connectionString"></param>
        public static void IdentityServerInMemory(this IServiceCollection services, string connectionString, bool sqlServer = false)
        {
            if (sqlServer)
            {
                services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer(connectionString));
            }
            else
            {
                services.AddDbContext <ApplicationDbContext>(options => options.UseSqlite(connectionString));
            }

            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(InMemoryConfig.GetIdentityResources())
            .AddInMemoryApiResources(InMemoryConfig.GetApiResources())
            .AddInMemoryClients(InMemoryConfig.GetClients())
            .AddAspNetIdentity <ApplicationUser>();

            InitUserDataBase(services.BuildServiceProvider());
        }
 public static void IdentityServerInMemory(this IServiceCollection services)
 {
     services.AddIdentityServer()
     .AddDeveloperSigningCredential()
     .AddInMemoryPersistedGrants()
     .AddInMemoryIdentityResources(InMemoryConfig.GetIdentityResources())
     .AddInMemoryApiResources(InMemoryConfig.GetApiResources())
     .AddInMemoryClients(InMemoryConfig.GetClients())
     .AddTestUsers(InMemoryConfig.GetUsers());
 }
        private static void InitDataBase(IServiceProvider service)
        {
            using (var scope = service.GetService <IServiceScopeFactory>().CreateScope())
            {
                scope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate();
                scope.ServiceProvider.GetRequiredService <ConfigurationDbContext>().Database.Migrate();
                scope.ServiceProvider.GetRequiredService <ApplicationDbContext>().Database.Migrate();

                var context = scope.ServiceProvider.GetRequiredService <ConfigurationDbContext>();

                if (!context.Clients.Any())
                {
                    foreach (var client in InMemoryConfig.GetClients())
                    {
                        context.Clients.Add(client.ToEntity());
                    }
                    context.SaveChanges();
                }

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

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

                var userManager = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                if (!userManager.Users.Any())
                {
                    foreach (var testUser in InMemoryConfig.GetUsers())
                    {
                        var identityUser = new ApplicationUser()
                        {
                            Id       = testUser.SubjectId,
                            UserName = testUser.Username
                        };

                        userManager.CreateAsync(identityUser, testUser.Password).Wait();
                        userManager.AddClaimsAsync(identityUser, testUser.Claims.ToList()).Wait();
                    }
                }
            }
        }