// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure <CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddIdentity <UserApp, IdentityRole>(o => { o.Password.RequireDigit = false; o.Password.RequireLowercase = false; o.Password.RequireUppercase = false; o.Password.RequireNonAlphanumeric = false; o.Password.RequiredLength = 6; }) .AddEntityFrameworkStores <ApplicationContext>() .AddDefaultTokenProviders(); services.AddDbContext <ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("StringConnection"))); services.AddAuthentication().AddCookie(); services.AddServerSideBlazor(); services.AddScoped <ITask, TaskDriver>(); services.AddMvc(); var provider = services.BuildServiceProvider(); //lately cut`s in another class for initialization StartsInitialize.Initialize(); DbInitializer dbInitializer = new DbInitializer(); dbInitializer.Initialize(provider); UserInitialize userInitialize = new UserInitialize(); userInitialize.Initialize(provider); RoleInitializer roleInitializer = new RoleInitializer(); roleInitializer.Initialize(provider); AdminInitialize adminInitialize = new AdminInitialize(); adminInitialize.Initialize(provider); services.AddHangfire((config) => { var options = new SqlServerStorageOptions { PrepareSchemaIfNecessary = true, QueuePollInterval = TimeSpan.FromHours(10) }; config.UseSqlServerStorage("Server=(localdb)\\mssqllocaldb;Database=CostsAnalyseDB;Trusted_Connection=True;MultipleActiveResultSets=true", options); }); }
public static void CreateAdminIfNotExists(IHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { AdminInitialize.InitializeAsync(services).Wait(); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred creating the Admin."); } } }