159 lines
6.2 KiB
C#

using Fab2ApprovalSystem.Misc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Configuration;
using Fab2ApprovalSystem.DMO;
using System.Web.Http;
using Fab2ApprovalSystem.JobSchedules;
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 DevAttachmentUrl = ConfigurationManager.AppSettings["DevAttachmentURl"].ToString();
//string ProdAttachmentUrl = ConfigurationManager.AppSettings["ProdAttachmentURL"].ToString();
string connectionstring = ConfigurationManager.ConnectionStrings["FabApprovalConnection"].ConnectionString.ToString();
GlobalVars.hostURL = HttpRuntime.AppDomainAppVirtualPath;
string DevWebSiteUrl = ConfigurationManager.AppSettings["DevWebSiteURL"].ToString();
string ProdWebSiteUrl = ConfigurationManager.AppSettings["ProdWebSiteURL"].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();
GlobalVars.DBConnection = connectionstring.ToUpper().Contains("TEST") ? "TEST" : connectionstring.ToUpper().Contains("QUALITY") ? "QUALITY" : "PROD";
//GlobalVars.AttachmentUrl = connectionstring.ToUpper().Contains("TEST") ? @"http://" + DevAttachmentUrl + "/" : @"http://" + ProdAttachmentUrl + "/"; ;
GlobalVars.hostURL = connectionstring.ToUpper().Contains("TEST") ? @"http://" + DevWebSiteUrl : @"http://" + ProdWebSiteUrl ;
#if (!DEBUG)
OOOTrainingReportJobSchedule.Start();
#endif
}
//void Application_BeginRequest(Object source, EventArgs e)
//{
// HttpApplication app = (HttpApplication)source;
// HttpContext context = app.Context;
// GlobalVars.hostURL = context.Request.Url.AbsoluteUri;
// // Attempt to peform first request initialization
//}
protected void Application_EndRequest()
{
var context = new HttpContextWrapper(Context);
//Do a direct 401 unautorized
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;
}
}
protected void Session_Start(object sender, EventArgs e)
{
Session[GlobalVars.ECN_VIEW_OPTION] = "Pending Approvals"; ;
}
protected void Session_End(object sender, EventArgs e)
{
//FormsAuthentication.SignOut();
try
{
Session[GlobalVars.SESSION_USERNAME] = "";
Session[GlobalVars.SESSION_USERID] = "";
Session[GlobalVars.IS_ADMIN] = "";
//LotTravelerDMO LotTravDMO = new LotTravelerDMO();
//LotTravDMO.ReleaseLockOnDocument((int)Session[GlobalVars.SESSION_USERID], -1);
}
catch(Exception ex)
{
Functions.WriteEvent(@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;
}
//Misc.Functions.WriteEvent(exString, System.Diagnostics.EventLogEntryType.Error);
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("Failed to write error to event log in database: " + ex2.ToString(), System.Diagnostics.EventLogEntryType.Error);
}
}
}
}