Program.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.EntityFrameworkCore;
  3. using VolgaIT.Data;
  4. var builder = WebApplication.CreateBuilder(args);
  5. var connectionString = builder.Configuration.GetConnectionString("ConnectionString");
  6. // Add services to the container.
  7. builder.Services.AddControllersWithViews();
  8. builder.Services.AddDbContext<DataContext>(opt =>
  9. {
  10. opt.UseNpgsql(connectionString);
  11. });
  12. builder.Services.AddIdentity<IdentityUser, IdentityRole>(opt=>
  13. {
  14. opt.User.RequireUniqueEmail = true;
  15. opt.Password.RequireDigit = false;
  16. opt.Password.RequiredLength = 3;
  17. opt.Password.RequireUppercase = false;
  18. opt.Password.RequireNonAlphanumeric = false;
  19. opt.Password.RequireLowercase = false;
  20. }).AddEntityFrameworkStores<DataContext>().AddTokenProvider<DataProtectorTokenProvider<IdentityUser>>(TokenOptions.DefaultProvider);
  21. builder.Services.AddScoped<DataContext>();
  22. var app = builder.Build();
  23. // Configure the HTTP request pipeline.
  24. if (!app.Environment.IsDevelopment())
  25. {
  26. app.UseExceptionHandler("/Home/Error");
  27. app.UseHsts();
  28. }
  29. app.UseHttpsRedirection();
  30. app.UseStaticFiles();
  31. app.UseRouting();
  32. app.UseAuthentication();
  33. app.UseAuthorization();
  34. app.MapControllerRoute(
  35. name: "default",
  36. pattern: "{controller=Home}/{action=Index}/{id?}");
  37. using (var scope = app.Services.CreateScope())
  38. {
  39. var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
  40. var context = scope.ServiceProvider.GetRequiredService<DataContext>();
  41. await DataBaseInit.InitDataBase(roleManager, context);
  42. }
  43. app.Run();