Mike Phares b586da5c82 Removed PdfViewController, HtmlViewRenderer and FakeView to be replaced with ViewEngineResult Render method
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
2025-05-23 12:27:09 -07:00

139 lines
6.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.Http;
#if !NET8
using System.Web.Mvc;
#endif
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using Fab2ApprovalSystem.DMO;
using Fab2ApprovalSystem.JobSchedules;
using Fab2ApprovalSystem.Misc;
using Fab2ApprovalSystem.Models;
namespace Fab2ApprovalSystem;
public class MvcApplication : System.Web.HttpApplication {
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
string hostName = System.Net.Dns.GetHostEntry("").HostName;
GlobalVars.IS_INFINEON_DOMAIN = hostName.ToLower().Contains("infineon");
string DevWebSiteUrl = ConfigurationManager.AppSettings["DevWebSiteURL"].ToString();
string ProdWebSiteUrlEC = ConfigurationManager.AppSettings["ProdWebSiteURLEC"].ToString();
string ProdWebSiteUrlStealth = ConfigurationManager.AppSettings["ProdWebSiteURLStealth"].ToString();
GlobalVars.SENDER_EMAIL = "FabApprovalSystem@Infineon.com"; // put in the Config File
if (ConfigurationManager.AppSettings["Notification Sender"] != null)
GlobalVars.SENDER_EMAIL = ConfigurationManager.AppSettings["Notification Sender"].ToString();
GlobalVars.NDriveURL = ConfigurationManager.AppSettings["NDrive"].ToString();
GlobalVars.WSR_URL = ConfigurationManager.AppSettings["WSR_URL"].ToString();
GlobalVars.CA_BlankFormsLocation = ConfigurationManager.AppSettings["CA_BlankFormsLocation"].ToString();
#if (!DEBUG)
OOOTrainingReportJobSchedule.Start();
if (GlobalVars.IS_INFINEON_DOMAIN) {
GlobalVars.DB_CONNECTION_STRING = ConfigurationManager.ConnectionStrings["FabApprovalConnectionStealth"].ConnectionString.ToString();
GlobalVars.hostURL = @"https://" + ProdWebSiteUrlStealth;
} else {
GlobalVars.DB_CONNECTION_STRING = ConfigurationManager.ConnectionStrings["FabApprovalConnectionEC"].ConnectionString.ToString();
GlobalVars.hostURL = @"https://" + ProdWebSiteUrlEC;
}
#else
GlobalVars.DB_CONNECTION_STRING = ConfigurationManager.ConnectionStrings["FabApprovalConnectionDev"].ConnectionString.ToString();
GlobalVars.hostURL = @"https://" + DevWebSiteUrl;
#endif
GlobalVars.DBConnection = GlobalVars.DB_CONNECTION_STRING.ToUpper().Contains("TEST") ? "TEST" : GlobalVars.DB_CONNECTION_STRING.ToUpper().Contains("QUALITY") ? "QUALITY" : "PROD";
GlobalVars.AppSettings = Models.AppSettings.LoadConfigurationManager();
}
protected void Application_EndRequest() {
var context = new HttpContextWrapper(Context);
#if !NET8
if (Context.Response.StatusCode == 301 && context.Request.IsAjaxRequest()) {
Context.Response.Clear();
Context.Response.StatusCode = 401;
} else if (FormsAuthentication.IsEnabled && context.Response.StatusCode == 302
&& context.Request.IsAjaxRequest()) {
context.Response.Clear();
context.Response.StatusCode = 401;
}
#endif
#if NET8
if (Context.Response.StatusCode == 301 && context.Request.Headers.TryGetValue("X-Requested-With", out Microsoft.Extensions.Primitives.StringValues strings) && strings[0] == "XMLHttpRequest") {
Context.Response.Clear();
Context.Response.StatusCode = 401;
} else if (FormsAuthentication.IsEnabled && context.Response.StatusCode == 302
&& context.Request.Headers.TryGetValue("X-Requested-With", out Microsoft.Extensions.Primitives.StringValues strings) && strings[0] == "XMLHttpRequest") {
context.Response.Clear();
context.Response.StatusCode = 401;
}
#endif
}
protected void Session_Start(object sender, EventArgs e) {
GlobalVars.SetECNViewOption(Session, "Pending Approvals");
}
protected void Session_End(object sender, EventArgs e) {
// FormsAuthentication.SignOut();
try {
GlobalVars.EndSession(Session);
} catch (Exception ex) {
Functions.WriteEvent(null, @User.Identity.Name + "\r\n Session Closed - \r\n" + ex.Message.ToString(), System.Diagnostics.EventLogEntryType.Error);
}
}
// This code is to allow hyperlinks from Office products to load the site without always forcing the user to log in
// It makes the browser reload the page so that the session cookies are sent properly
private static string MSUserAgentsRegex = @"[^\w](Word|Excel|PowerPoint|ms-office)([^\w]|\z)";
protected void Application_OnPostAuthenticateRequest(object sender, EventArgs e) {
if (System.Text.RegularExpressions.Regex.IsMatch(Request.UserAgent, MSUserAgentsRegex)) {
Response.Write("<html><head><meta http-equiv='refresh' content='0'/></head><body></body></html>");
Response.End();
}
}
void Application_Error(object sender, EventArgs e) {
var ex = Server.GetLastError();
var exString = "Caught unhandled exception:\r\n";
exString += String.Format("User: {0}\r\n", @User.Identity.Name);
Exception x = ex;
while (x != null) {
exString += x.ToString();
exString += "=====\r\n";
x = x.InnerException;
}
try {
if (exString.Length > 500)
exString = exString.Substring(0, 500);
EventLogDMO.Add(new Fab2ApprovalSystem.Models.WinEventLog() {
UserID = @User.Identity.Name,
OperationType = "Error",
Comments = exString
});
} catch (Exception ex2) {
Misc.Functions.WriteEvent(null, "Failed to write error to event log in database: " + ex2.ToString(), System.Diagnostics.EventLogEntryType.Error);
}
}
}