Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Duende.IdentityServer;
- using Duende.IdentityServer.EntityFramework.DbContexts;
- using Duende.IdentityServer.EntityFramework.Mappers;
- using HeadhuntNow.Auth.CustomValidators;
- using HeadhuntNow.Auth.Data;
- using HeadhuntNow.Auth.Models;
- using Microsoft.AspNetCore.Identity;
- using Microsoft.EntityFrameworkCore;
- using Serilog;
- namespace HeadhuntNow.Auth;
- internal static class HostingExtensions
- {
- public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
- {
- builder.Services.AddRazorPages();
- builder.Services.AddSingleton<Config>();
- builder.Services.AddDbContext<ApplicationDbContext>(
- options =>
- options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
- o => { o.EnableRetryOnFailure(5); }));
- builder.Services.AddIdentity<ApplicationUser, IdentityRole>(
- options =>
- {
- options.User.AllowedUserNameCharacters =
- "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+æøåÆØÅ";
- })
- .AddPasswordValidator<CustomPasswordValidator<ApplicationUser>>()
- .AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
- builder.Services
- .AddIdentityServer(
- options =>
- {
- options.Events.RaiseErrorEvents = true;
- options.Events.RaiseInformationEvents = true;
- options.Events.RaiseFailureEvents = true;
- options.Events.RaiseSuccessEvents = true;
- options.EmitStaticAudienceClaim = true;
- })
- .AddConfigurationStore(
- options =>
- {
- options.ConfigureDbContext = b =>
- b.UseSqlServer(
- builder.Configuration.GetConnectionString("DefaultConnection"),
- sql => sql.MigrationsAssembly(typeof(Program).Assembly.FullName));
- })
- .AddOperationalStore(
- options =>
- {
- options.ConfigureDbContext = b =>
- b.UseSqlServer(
- builder.Configuration.GetConnectionString("DefaultConnection"),
- sql => sql.MigrationsAssembly(typeof(Program).Assembly.FullName));
- })
- .AddAspNetIdentity<ApplicationUser>()
- ;
- //TODO: Might need to be moved to the front-end
- builder.Services.AddCors(options =>
- {
- options.AddPolicy(
- "BlazorPolicy",
- b =>
- {
- b.WithOrigins("https://localhost:5137")
- .AllowAnyHeader()
- .AllowAnyMethod()
- .AllowCredentials();
- });
- });
- return builder.Build();
- }
- public static WebApplication ConfigurePipeline(this WebApplication app)
- {
- app.UseSerilogRequestLogging();
- if (app.Environment.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- app.UseCsp(
- m =>
- {
- m.BlockAllMixedContent()
- .FrameAncestors(
- s =>
- {
- s.Self();
- s.CustomSources(
- "https://localhost:*"
- );
- })
- .ScriptSources(
- s =>
- {
- s.Self();
- s.UnsafeInline();
- })
- .StyleSources(
- s =>
- {
- s.Self();
- s.UnsafeInline();
- });
- }
- );
- InitializeDatabase(app);
- app.UseCors("BlazorPolicy");
- app.UseStaticFiles();
- app.UseRouting();
- app.UseIdentityServer();
- app.UseAuthorization();
- app.MapRazorPages()
- .RequireAuthorization();
- return app;
- }
- private static void InitializeDatabase(IApplicationBuilder app)
- {
- using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
- {
- var config = serviceScope.ServiceProvider.GetRequiredService<Config>();
- serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>()
- .Database.Migrate();
- serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>()
- .Database.Migrate();
- var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
- context.Database.Migrate();
- context.Clients.RemoveRange(context.Clients);
- context.SaveChanges();
- if (!context.Clients.Any())
- {
- foreach (var client in config.GetClients())
- {
- context.Clients.Add(client.ToEntity());
- }
- context.SaveChanges();
- }
- context.IdentityResources.RemoveRange(context.IdentityResources);
- context.SaveChanges();
- if (!context.IdentityResources.Any())
- {
- foreach (var resource in config.IdentityResources)
- {
- context.IdentityResources.Add(resource.ToEntity());
- }
- context.SaveChanges();
- }
- context.ApiScopes.RemoveRange(context.ApiScopes);
- context.SaveChanges();
- if (!context.ApiScopes.Any())
- {
- foreach (var resource in config.ApiScopes)
- {
- context.ApiScopes.Add(resource.ToEntity());
- }
- context.SaveChanges();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment