Files
ayanova7/source/ri/ri/Global.asax.cs
2018-06-29 19:47:36 +00:00

150 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using GZTW.AyaNova.BLL;
using System.Threading;
using System.Diagnostics;
using ri.util;
namespace ri
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// AuthConfig.RegisterAuth();
AYINIT();
}
private void AYINIT()
{
if (Initialized) return;
//try init
try
{
GZTW.AyaNova.BLL.AyaBizUtils.Initialize();
Initialized = true;
}
catch (Exception ex)
{
//"crack" the exception to get to the innermost exception
while (ex.InnerException != null)
ex = ex.InnerException;
throw (ex);
}
}
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
SetPrincipal();
}
private static bool Initialized = false;
protected void SetPrincipal()
{
AYINIT();
if (System.Web.HttpContext.Current.Session == null)
{
return;
}
if (System.Web.HttpContext.Current.Session["AyaNova-Principle"] != null)
{
Thread.CurrentPrincipal = (System.Security.Principal.IPrincipal)System.Web.HttpContext.Current.Session["AyaNova-Principle"];
HttpContext.Current.User = (System.Security.Principal.IPrincipal)System.Web.HttpContext.Current.Session["AyaNova-Principle"];
}
else
{//No session in state so redirect to login, but ONLY if not already going to the login page
//modified block below to ensure customer route requests not logged in go to customer login
//and non customer route requests go to employee login
if (! Request.AppRelativeCurrentExecutionFilePath.Contains("Login"))//case 2033
{
if (!Request.AppRelativeCurrentExecutionFilePath.Contains("Customer"))
Response.Redirect("~/Login/Login");
else
Response.Redirect("~/Customer/Login");
}
}
}
//DURING EARLY BETA TESTING THIS IS OFF ON PURPOSE.
//TODO: re-enable this before release but after initial beta testing
#if(DEBUG)
void Application_Error(Object sender, EventArgs e)
{
//global error handling, inspired by: http://forums.asp.net/t/1505777.aspx?Error+Handling+in+global+asax
Exception ex = Server.GetLastError();
Response.Clear();
HttpException httpException = ex as HttpException;
if (httpException != null)
{
bool notfound;
switch (httpException.GetHttpCode())
{
case 404:
notfound = true;
break;
default:
notfound = false;
break;
}
Server.ClearError();
if (notfound)
this.Response.RedirectToRoute("Default", new { controller = "Home", action = "NotFound", req = Request.Url });
else
{
ay.cacheData("exmsg", ex);
this.Response.RedirectToRoute("Default", new { controller = "Home", action = "Error" });
}
}
else
{
if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
{
Server.ClearError();
ay.cacheData("exmsg", ex);
this.Response.RedirectToRoute("Default", new { controller = "Home", action = "Error" });
}
}
}
#endif
//eoc
}
}