private static async Task SeedData(IWebHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var _userManager = services.GetRequiredService <UserManager <ApplicationUser> >(); var _roleManager = services.GetRequiredService <RoleManager <IdentityRole> >(); await IdentityDataInitializer.SeedDataAsync(_userManager, _roleManager); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred while seeding the database."); } } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure( IApplicationBuilder app, UserManager <User> userManager, RoleManager <Role> roleManager) { app.UseExceptionHandler("/api/error"); app.UseHttpsRedirection(); // Needed for hosting in Azure Web App var forwardOptions = new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto, RequireHeaderSymmetry = false }; forwardOptions.KnownNetworks.Clear(); forwardOptions.KnownProxies.Clear(); app.UseForwardedHeaders(forwardOptions); app.UseRouting(); app.UseIdentityServer(); app.UseAuthentication(); app.UseAuthorization(); // Seed default Roles and Users IdentityDataInitializer.SeedDataAsync(userManager, roleManager, _configuration).Wait(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseSwagger(); app.UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "Caff Store API v1"); options.OAuthClientId("swagger"); options.DisplayOperationId(); }); }