Added HttpException class for missing HttpException for net8 Wrapped HttpContext.Session, GetJsonResult, IsAjaxRequest and GetUserIdentityName in controllers for net8 Added AuthenticationService to test Fab2ApprovalMKLink code for net8 Compile conditionally flags to debug in dotnet core
134 lines
7.1 KiB
C#
134 lines
7.1 KiB
C#
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<Program>? logger = null;
|
|
WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder(args);
|
|
_ = webApplicationBuilder.Configuration.AddUserSecrets<Program>();
|
|
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<ApiBehaviorOptions>(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<ICompositeViewEngine, CompositeViewEngine>();
|
|
// _ = webApplicationBuilder.Services.AddTransient<IViewRenderingService, ViewRenderingService>();
|
|
// _ = webApplicationBuilder.Services.AddScoped<IViewRenderService, ViewRenderService>();
|
|
// _ = webApplicationBuilder.Services.AddSingleton<ITempDataProvider, ITempDataProvider>();
|
|
_ = webApplicationBuilder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
_ = webApplicationBuilder.Services.AddScoped<IAuthenticationService, AuthenticationService>();
|
|
_ = webApplicationBuilder.Services.AddScoped<IDalService, DalService>();
|
|
_ = webApplicationBuilder.Services.AddScoped<IDbConnectionService, DbConnectionService>();
|
|
_ = webApplicationBuilder.Services.AddScoped<IUserService, UserService>();
|
|
_ = 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<IHostLifetime, WindowsServiceLifetime>();
|
|
_ = 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<AppSettings>();
|
|
logger = webApplication.Services.GetRequiredService<ILogger<Program>>();
|
|
_ = 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;
|
|
}
|
|
}
|
|
|
|
} |