using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; using Sleis.Filter; using Sleis.Infrastructure; using System.Web.Security; using System.Security.Principal; using System.Diagnostics; using Sleis.Models; using Sleis.Service; using Sleis.Validation.Attribute; using Sleis.Binders; using Sleis.Utility; using log4net; namespace Sleis { public class MvcApplication : System.Web.HttpApplication { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new GenericLoggingFilter()); filters.Add(new ExceptionLoggingFilter()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); //needs to be first so that it gets handled prior to default route. routes.MapRoute( null, "Page/Help/{ctrl}", new { controller = "Page", action = "Help", ctrl = UrlParameter.Optional } ); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Public", action = "Home", id = UrlParameter.Optional } // Parameter defaults ); routes.MapRoute( "Help", // Route name "{controller}/{action}/{ctrl}/{act}", // URL with parameters new { controller = "Page", action = "Help", ctrl = UrlParameter.Optional, act = UrlParameter.Optional } // Parameter defaults ); routes.MapRoute( "Lookup", // Route name "{controller}/{action}/{type}/{query}/{year}/{size}/{facilityId}", // URL with parameters new { controller = "Lookup", action = "Get", type = UrlParameter.Optional, query = UrlParameter.Optional, year = UrlParameter.Optional, size = UrlParameter.Optional, facilityId = UrlParameter.Optional } // Parameter defaults string type, string query, int size = 100 ); } public override void Init() { this.BeginRequest += new EventHandler(MvcApplication_BeginRequest); base.Init(); } /* protected void Session_End() { Response.Redirect("Account/Login"); } * */ /*BO - 3-28-2013 - I added this method and setting User here because for some reason, when deploying to IISDEV, user was always null when authorizing applications. * I am not entirely sure, but *guessing* that there was some change to .NET that causes User to get lost in the chain events if you set User during BeginRequest below... * Will need to keep an eye on this*/ protected void Application_AuthenticateRequest(Object sender, EventArgs e) { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { ILog log = LogManager.GetLogger("Global ASAX"); SleisVisit visit = Utility.AppUtility.GetVisit(); log.InfoFormat("MvcApplication_AuthenticateRequest. SleisVist: {0}", visit.ToString()); Context.User = new GenericPrincipal(visit as IIdentity, null); } } void MvcApplication_BeginRequest(object sender, EventArgs e) { ILog log = LogManager.GetLogger("Global ASAX"); SleisVisit visit = Utility.AppUtility.GetVisit(); log.InfoFormat("MvcApplication_BeginRequest. SleisVist: {0}", visit.ToString()); HttpContext.Current.User = new GenericPrincipal(visit as IIdentity, null); IPropertyPlaceholder Properties = SpringServiceProvider.GetService("deploymentVars"); if (( String.IsNullOrEmpty(Properties.Get("app.disable.https.auto.redirect")) || Properties.Get("app.disable.https.auto.redirect").ToLower() != "true" ) && Properties.Get("app.base.url").StartsWith("https")) { switch (Request.Url.Scheme) { case "https": Response.AddHeader("Strict-Transport-Security", "max-age=300"); break; case "http": var path = "https://" + Request.Url.Host + Request.Url.PathAndQuery; Response.Status = "301 Moved Permanently"; Response.AddHeader("Location", path); break; } } } protected void Application_Start() { //Configure logging log4net.Config.XmlConfigurator.Configure(); //Set Custom Resolver DependencyResolver.SetResolver(new CustomServiceResolver(DependencyResolver.Current)); //Register areas AreaRegistration.RegisterAllAreas(); //Register filters like logging on unhandled error RegisterGlobalFilters(GlobalFilters.Filters); //Register the default rout RegisterRoutes(RouteTable.Routes); ModelBinders.Binders.Add(typeof(FacilityContactMethodModel), new UserContactMethodBinder()); ModelBinders.Binders.Add(typeof(Identifier), new IdentifierBinder()); ModelBinders.Binders.Add(typeof(UnitProcess), new UnitProcessBinder()); ModelBinders.Binders.Add(typeof(EmissionUnit), new EmissionUnitBinder()); ModelBinders.Binders.Add(typeof(ControlDevicePollutant), new ControlDevicePollutantBinder()); ModelBinders.Binders.Add(typeof(ControlDeviceModel), new ControlDeviceBinder()); ModelBinders.Binders.Add(typeof(ReleasePoint), new ReleasePointBinder()); ModelBinders.Binders.Add(typeof(UnitProcessReleasePoint), new UnitProcessReleasePointBinder()); ModelBinders.Binders.Add(typeof(UnitProcessRegulationProgram), new UnitProcessRegulationProgramBinder()); ModelBinders.Binders.Add(typeof(FacilityModel), new FacilityBinder()); ModelBinders.Binders.Add(typeof(EmissionUnitControlApproach), new EmissionUnitControlApproachBinder()); ModelBinders.Binders.Add(typeof(EmissionUnitRegulationProgram), new EmissionUnitRegulationProgramBinder()); ModelBinders.Binders.Add(typeof(ProcessEmission), new ProcessEmissionBinder()); ModelBinders.Binders.Add(typeof(Emission), new EmissionBinder()); ModelBinders.Binders.Add(typeof(EntityControlDevice), new EntityControlDeviceBinder()); ModelBinders.Binders.Add(typeof(UnitProcessControlApproach), new UnitProcessControlApproachBinder()); ModelBinders.Binders.Add(typeof(Comment), new CommentBinder()); // register custom model validators //DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DecimalValidationAttribute), typeof(DecimalValidator)); //DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(DateValidation), typeof(DateValidator)); } } }