public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var userManager = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                var ctx         = scope.ServiceProvider.GetRequiredService <AppDbContext>();

                var testUser = new ApplicationUser("test")
                {
                    Email = "*****@*****.**"
                };
                userManager.CreateAsync(testUser, "password").GetAwaiter().GetResult();

                // var testProfile = new UserProfile
                // {
                //     ApplicationUser = testUser,
                // };
                //
                // ctx.UserProfiles.AddAsync(testProfile).GetAwaiter().GetResult();

                var mod = new ApplicationUser("mod")
                {
                    Email = "*****@*****.**"
                };

                userManager.CreateAsync(mod, "password").GetAwaiter().GetResult();
                userManager.AddClaimAsync(mod, new Claim(ApiConstants.Claims.Role, ApiConstants.Roles.Mod)).GetAwaiter().GetResult();

                // var modProfile = new UserProfile
                // {
                //     ApplicationUser = mod,
                // };
                //
                // ctx.UserProfiles.AddAsync(modProfile).GetAwaiter().GetResult();

                scope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.EnsureCreated();

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

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

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

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

                if (!context.ApiScopes.Any())
                {
                    foreach (var resource in IdentityConfig.GetApiScopes())
                    {
                        context.ApiScopes.Add(resource.ToEntity());
                    }
                    context.SaveChanges();
                }
            }

            host.Run();
        }
        private void AddIdentity(IServiceCollection services)
        {
            services.AddIdentity <ApplicationUser, IdentityRole>(options =>
            {
                options.User.RequireUniqueEmail = true;
                if (_env.IsDevelopment())
                {
                    options.Password.RequireDigit           = false;
                    options.Password.RequiredLength         = 6;
                    options.Password.RequireUppercase       = false;
                    options.Password.RequireLowercase       = false;
                    options.Password.RequireNonAlphanumeric = false;
                }
                else
                {
                    // add in configs for production
                }
            })
            .AddEntityFrameworkStores <AppDbContext>()
            .AddDefaultTokenProviders();


            services.ConfigureApplicationCookie(config =>
            {
                config.LoginPath  = "/account/login";
                config.LogoutPath = "/api/auth/logout";
            });

            var identityServiceBuilder = services.AddIdentityServer();

            identityServiceBuilder.AddAspNetIdentity <ApplicationUser>();

            var assembly = typeof(Startup).Assembly.GetName().Name;

            if (_env.IsDevelopment())
            {
                identityServiceBuilder
                .AddConfigurationStore(options =>
                {
                    options.ConfigureDbContext = b => b.UseSqlServer(_configuration.GetConnectionString("AppDbContext"),
                                                                     sql => sql.MigrationsAssembly(assembly));
                })
                .AddOperationalStore(options =>
                {
                    options.ConfigureDbContext = b => b.UseSqlServer(_configuration.GetConnectionString("AppDbContext"),
                                                                     sql => sql.MigrationsAssembly(assembly));
                })
                .AddInMemoryIdentityResources(IdentityConfig.GetIdentityResources())
                .AddInMemoryClients(IdentityConfig.GetClients())
                .AddInMemoryApiScopes(IdentityConfig.GetApiScopes());

                identityServiceBuilder.AddDeveloperSigningCredential();
            }

            services.AddLocalApiAuthentication();

            services.AddAuthentication("CookieAuth")
            .AddCookie("CookieAuth", config =>
            {
                config.Cookie.Name = "server";
                config.LoginPath   = "/account/login";
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy(IdentityServerConstants.LocalApi.PolicyName, policy =>
                {
                    policy.AddAuthenticationSchemes(IdentityServerConstants.LocalApi.AuthenticationScheme);
                    policy.RequireAuthenticatedUser();
                });

                options.AddPolicy(ApiConstants.Policies.Mod, policy =>
                {
                    var isPolicy = options.DefaultPolicy;
                    policy.Combine(isPolicy);
                    policy.RequireClaim(ApiConstants.Claims.Role, ApiConstants.Roles.Mod);
                    policy.RequireAuthenticatedUser();
                });
            });
        }