using Microsoft.EntityFrameworkCore; using DigitalData.UserManager.Infrastructure.Repositories; using DigitalData.UserManager.Application; using DigitalData.Core.Application; using Microsoft.AspNetCore.Authentication.Cookies; using NLog.Web; using NLog; using DigitalData.Core.API; var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger(); logger.Debug("init main"); try { var builder = WebApplication.CreateBuilder(args); if (builder.Configuration.GetValue("RunAsWindowsService")) builder.Host.UseWindowsService(); builder.Logging.ClearProviders(); builder.Host.UseNLog(); builder.Services.AddControllers(); if (builder.Configuration.GetValue("UseSwagger")) { builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); } builder.Services.AddControllers(); builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.Cookie.HttpOnly = true; // Makes the cookie inaccessible to client-side scripts for security options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; // Ensures cookies are sent over HTTPS only options.Cookie.SameSite = SameSiteMode.Strict; // Protects against CSRF attacks by restricting how cookies are sent with requests from external sites options.LoginPath = "/api/auth/login"; options.LogoutPath = "/api/auth/logout"; }); builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DD_ECM_Connection")) .EnableDetailedErrors()); var allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get() ?? throw new InvalidOperationException("In appsettings there is no allowed origin."); builder.Services.AddCors(options => { options.AddPolicy(name: "DefaultCorsPolicy", builder => { builder.WithOrigins(allowedOrigins) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); }); }); //builder.Services.AddAutoMapper(typeof(DirectoryMappingProfile).Assembly); builder.Services.AddUserManager(); builder.Services.AddDirectorySearchService(); builder.Services.AddCookieBasedLocalizer(); var app = builder.Build(); app.UseCors("DefaultCorsPolicy"); if (builder.Configuration.GetValue("UseSwagger")) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseCookieBasedLocalizer("de-DE", "en-US"); app.UseDefaultFiles(); app.UseStaticFiles(); app.UseRouting(); app.UseHttpsRedirection(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.MapDefaultControllerRoute(); app.Run(); } catch (Exception exception) { logger.Error(exception, "Stopped program because of exception"); throw; }