Guest User

Untitled

a guest
Nov 3rd, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.20 KB | None | 0 0
  1. using Duende.IdentityServer;
  2. using Duende.IdentityServer.EntityFramework.DbContexts;
  3. using Duende.IdentityServer.EntityFramework.Mappers;
  4. using HeadhuntNow.Auth.CustomValidators;
  5. using HeadhuntNow.Auth.Data;
  6. using HeadhuntNow.Auth.Models;
  7. using Microsoft.AspNetCore.Identity;
  8. using Microsoft.EntityFrameworkCore;
  9. using Serilog;
  10.  
  11. namespace HeadhuntNow.Auth;
  12.  
  13. internal static class HostingExtensions
  14. {
  15.     public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
  16.     {
  17.         builder.Services.AddRazorPages();
  18.  
  19.         builder.Services.AddSingleton<Config>();
  20.         builder.Services.AddDbContext<ApplicationDbContext>(
  21.             options =>
  22.                 options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
  23.                     o => { o.EnableRetryOnFailure(5); }));
  24.  
  25.         builder.Services.AddIdentity<ApplicationUser, IdentityRole>(
  26.                 options =>
  27.                 {
  28.                     options.User.AllowedUserNameCharacters =
  29.                         "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+æøåÆØÅ";
  30.                 })
  31.             .AddPasswordValidator<CustomPasswordValidator<ApplicationUser>>()
  32.             .AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
  33.  
  34.         builder.Services
  35.             .AddIdentityServer(
  36.                 options =>
  37.                 {
  38.                     options.Events.RaiseErrorEvents = true;
  39.                     options.Events.RaiseInformationEvents = true;
  40.                     options.Events.RaiseFailureEvents = true;
  41.                     options.Events.RaiseSuccessEvents = true;
  42.  
  43.                     options.EmitStaticAudienceClaim = true;
  44.                 })
  45.             .AddConfigurationStore(
  46.                 options =>
  47.                 {
  48.                     options.ConfigureDbContext = b =>
  49.                         b.UseSqlServer(
  50.                             builder.Configuration.GetConnectionString("DefaultConnection"),
  51.                             sql => sql.MigrationsAssembly(typeof(Program).Assembly.FullName));
  52.                 })
  53.             .AddOperationalStore(
  54.                 options =>
  55.                 {
  56.                     options.ConfigureDbContext = b =>
  57.                         b.UseSqlServer(
  58.                             builder.Configuration.GetConnectionString("DefaultConnection"),
  59.                             sql => sql.MigrationsAssembly(typeof(Program).Assembly.FullName));
  60.                 })
  61.             .AddAspNetIdentity<ApplicationUser>()
  62.             ;
  63.  
  64.         //TODO: Might need to be moved to the front-end
  65.         builder.Services.AddCors(options =>
  66.         {
  67.             options.AddPolicy(
  68.                 "BlazorPolicy",
  69.                 b =>
  70.                 {
  71.                     b.WithOrigins("https://localhost:5137")
  72.                         .AllowAnyHeader()
  73.                         .AllowAnyMethod()
  74.                         .AllowCredentials();
  75.                 });
  76.         });
  77.         return builder.Build();
  78.     }
  79.  
  80.     public static WebApplication ConfigurePipeline(this WebApplication app)
  81.     {
  82.         app.UseSerilogRequestLogging();
  83.  
  84.         if (app.Environment.IsDevelopment())
  85.         {
  86.             app.UseDeveloperExceptionPage();
  87.         }
  88.  
  89.         app.UseCsp(
  90.             m =>
  91.             {
  92.                 m.BlockAllMixedContent()
  93.                     .FrameAncestors(
  94.                         s =>
  95.                         {
  96.                             s.Self();
  97.                             s.CustomSources(
  98.                                 "https://localhost:*"
  99.                             );
  100.                         })
  101.                     .ScriptSources(
  102.                         s =>
  103.                         {
  104.                             s.Self();
  105.                             s.UnsafeInline();
  106.                         })
  107.                     .StyleSources(
  108.                         s =>
  109.                         {
  110.                             s.Self();
  111.                             s.UnsafeInline();
  112.                         });
  113.             }
  114.         );
  115.         InitializeDatabase(app);
  116.         app.UseCors("BlazorPolicy");
  117.  
  118.         app.UseStaticFiles();
  119.         app.UseRouting();
  120.         app.UseIdentityServer();
  121.         app.UseAuthorization();
  122.  
  123.         app.MapRazorPages()
  124.             .RequireAuthorization();
  125.  
  126.         return app;
  127.     }
  128.  
  129.     private static void InitializeDatabase(IApplicationBuilder app)
  130.     {
  131.         using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
  132.         {
  133.             var config = serviceScope.ServiceProvider.GetRequiredService<Config>();
  134.             serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>()
  135.                 .Database.Migrate();
  136.  
  137.             serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>()
  138.                 .Database.Migrate();
  139.  
  140.             var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
  141.             context.Database.Migrate();
  142.             context.Clients.RemoveRange(context.Clients);
  143.             context.SaveChanges();
  144.             if (!context.Clients.Any())
  145.             {
  146.                 foreach (var client in config.GetClients())
  147.                 {
  148.                     context.Clients.Add(client.ToEntity());
  149.                 }
  150.  
  151.                 context.SaveChanges();
  152.             }
  153.  
  154.             context.IdentityResources.RemoveRange(context.IdentityResources);
  155.             context.SaveChanges();
  156.             if (!context.IdentityResources.Any())
  157.             {
  158.                 foreach (var resource in config.IdentityResources)
  159.                 {
  160.                     context.IdentityResources.Add(resource.ToEntity());
  161.                 }
  162.  
  163.                 context.SaveChanges();
  164.             }
  165.  
  166.             context.ApiScopes.RemoveRange(context.ApiScopes);
  167.             context.SaveChanges();
  168.             if (!context.ApiScopes.Any())
  169.             {
  170.                 foreach (var resource in config.ApiScopes)
  171.                 {
  172.                     context.ApiScopes.Add(resource.ToEntity());
  173.                 }
  174.  
  175.                 context.SaveChanges();
  176.             }
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment