using HN.Application; using HN.Domain; using HN.Infrastructure; using HN.Infrastructure.Behaviors; using HN.Infrastructure.Identity; using HN.Infrastructure.Repositories; using MediatR; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.AspNetCore.Routing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Website { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlite(Configuration.GetConnectionString("Default"))); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(typeof(IPipelineBehavior<,>), typeof(UnitOfWorkBehavior<,>)); services.AddMediatR(typeof(HN.Application.IHNContext)); // Permet d'avoir des routes en lowercase services.Configure(options => { options.LowercaseUrls = true; options.LowercaseQueryStrings = true; }); // Pour permettre l'authentification services.AddIdentity(o => { o.Password.RequiredLength = o.Password.RequiredUniqueChars = 0; o.Password.RequireDigit = o.Password.RequireLowercase = o.Password.RequireNonAlphanumeric = o.Password.RequireUppercase = false; }) .AddEntityFrameworkStores(); // Permet de reconfigurer certaines parties préconfigurées par Identity https://github.com/dotnet/aspnetcore/blob/3ea1fc7aac9d43152908d5d45ae811f3df7ca399/src/Identity/Core/src/IdentityServiceCollectionExtensions.cs#L51 services.PostConfigure(IdentityConstants.ApplicationScheme, o => { o.LoginPath = "/accounts/login"; o.LogoutPath = "/accounts/logout"; }); services.AddControllersWithViews(o => { o.Filters.Add(); o.Filters.Add(new AuthorizeFilter()); // Nécessite l'authentification par défaut }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { MigrateDatabase(app); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } // app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); // Permet de rediriger selon les codes d'erreurs retournés, notamment par notre CustomExceptionFilter app.UseStatusCodePages(context => { var request = context.HttpContext.Request; var response = context.HttpContext.Response; if (response.StatusCode == (int)System.Net.HttpStatusCode.Unauthorized) { response.Redirect("/accounts/login"); } return System.Threading.Tasks.Task.CompletedTask; }); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Links}/{action=Index}/{id?}"); }); } /// /// Lance les migrations. En production, il est plutôt conseillé de générer /// les scripts avec `dotnet ef migrations script` et de les passer à la main. /// /// private void MigrateDatabase(IApplicationBuilder app) { using var scope = app.ApplicationServices.CreateScope(); using var ctx = scope.ServiceProvider.GetRequiredService(); ctx.Database.Migrate(); } } }