using System; using System.Diagnostics; using System.Text; using Fab2ApprovalSystem.Misc; using Fab2ApprovalSystem.Models; using Fab2ApprovalSystem.Services; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ViewEngines; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting.WindowsServices; using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Tokens; namespace Fab2ApprovalMKLink; public class Program { public static int Main(string[] args) { ILogger? logger = null; WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder(args); _ = webApplicationBuilder.Configuration.AddUserSecrets(); AppSettings appSettings = AppSettings.Get(webApplicationBuilder.Configuration); if (string.IsNullOrEmpty(appSettings.Company)) throw new Exception("Company name must have a value!"); if (string.IsNullOrEmpty(appSettings.WorkingDirectoryName)) throw new Exception("Working directory name must have a value!"); GlobalVars.AppSettings = appSettings; GlobalVars.AttachmentUrl = appSettings.AttachmentUrl is null ? string.Empty : appSettings.AttachmentUrl; GlobalVars.CA_BlankFormsLocation = appSettings.CABlankFormsLocation; GlobalVars.DBConnection = appSettings.DBConnection; GlobalVars.DB_CONNECTION_STRING = appSettings.DBConnectionString; GlobalVars.hostURL = appSettings.HostURL; GlobalVars.IS_INFINEON_DOMAIN = appSettings.IsInfineonDomain; GlobalVars.MesaTemplateFiles = appSettings.MesaTemplateFiles; GlobalVars.NDriveURL = appSettings.NDriveURL; GlobalVars.SENDER_EMAIL = appSettings.SenderEmail; GlobalVars.USER_ID = appSettings.UserId; GlobalVars.USER_ISADMIN = appSettings.UserIsAdmin; GlobalVars.WSR_URL = appSettings.WSR_URL; try { _ = webApplicationBuilder.Services.Configure(options => options.SuppressModelStateInvalidFilter = true); _ = webApplicationBuilder.Services.AddControllers(); _ = webApplicationBuilder.Services.AddControllersWithViews(); _ = webApplicationBuilder.Services.AddDistributedMemoryCache(); _ = webApplicationBuilder.Services.AddHttpClient(); _ = webApplicationBuilder.Services.AddMemoryCache(); _ = webApplicationBuilder.Services.AddSingleton(_ => appSettings); _ = webApplicationBuilder.Services.AddSingleton(); // _ = webApplicationBuilder.Services.AddTransient(); // _ = webApplicationBuilder.Services.AddScoped(); // _ = webApplicationBuilder.Services.AddSingleton(); _ = webApplicationBuilder.Services.AddSingleton(); _ = webApplicationBuilder.Services.AddScoped(); _ = webApplicationBuilder.Services.AddScoped(); _ = webApplicationBuilder.Services.AddScoped(); _ = webApplicationBuilder.Services.AddScoped(); _ = webApplicationBuilder.Services.AddSwaggerGen(); _ = webApplicationBuilder.Services.AddSession(sessionOptions => { sessionOptions.IdleTimeout = TimeSpan.FromSeconds(2000); sessionOptions.Cookie.HttpOnly = true; sessionOptions.Cookie.IsEssential = true; } ); _ = webApplicationBuilder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, ValidIssuer = appSettings.JwtIssuer, ValidateAudience = false, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(appSettings.JwtKey)), ClockSkew = TimeSpan.Zero }; }); _ = webApplicationBuilder.Services.AddAuthorization(options => { options.DefaultPolicy = new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser() .Build(); }); if (WindowsServiceHelpers.IsWindowsService()) { _ = webApplicationBuilder.Services.AddSingleton(); _ = webApplicationBuilder.Logging.AddEventLog(settings => { #pragma warning disable CA1416 if (string.IsNullOrEmpty(settings.SourceName)) settings.SourceName = webApplicationBuilder.Environment.ApplicationName; #pragma warning restore }); } WebApplication webApplication = webApplicationBuilder.Build(); if (Debugger.IsAttached) webApplication.Services.GetRequiredService(); logger = webApplication.Services.GetRequiredService>(); _ = webApplication.UseCors(corsPolicyBuilder => corsPolicyBuilder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); if (!webApplicationBuilder.Environment.IsDevelopment()) { _ = webApplication.UseExceptionHandler("/Error"); _ = webApplication.UseHttpsRedirection(); _ = webApplication.UseHsts(); } else { if (string.IsNullOrEmpty(appSettings.URLs)) { Environment.ExitCode = -1; webApplication.Lifetime.StopApplication(); } _ = webApplication.UseSwagger(); _ = webApplication.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Server V1")); } _ = webApplication.UseSession(); _ = webApplication.MapControllers(); _ = webApplication.UseAuthentication(); _ = webApplication.UseAuthorization(); logger.LogInformation("Starting Web Application"); webApplication.Run(); return 0; } catch (Exception ex) { try { logger?.LogCritical(ex, "Host terminated unexpectedly"); } catch (Exception) { } throw; } } }