protected void Application_BeginRequest(object sender, EventArgs e) { GlobalSettings.Exigo.VerifyEnvironment(HttpContext.Current); if (Request.IsSecureConnection) { Response.AddHeader("Strict-Transport-Security", "max-age=31536000"); } // Get the route data var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current)); // Account for attribute routing and null routeData if (routeData != null && routeData.Values.ContainsKey("MS_DirectRouteMatches")) { routeData = ((List <RouteData>)routeData.Values["MS_DirectRouteMatches"]).First(); } // If we have an identity and the current identity matches the web alias in the routes, stop here. var identity = HttpContext.Current.Items["OwnerWebIdentity"] as ReplicatedSiteIdentity; if (routeData == null || routeData.Values["webalias"] == null || (identity != null && identity.WebAlias.Equals(routeData.Values["webalias"].ToString(), StringComparison.InvariantCultureIgnoreCase))) { return; } // Determine some web alias data var urlHelper = new UrlHelper(new RequestContext(new HttpContextWrapper(HttpContext.Current), RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current)))); var currentWebAlias = routeData.Values["webalias"].ToString(); var defaultWebAlias = GlobalSettings.ReplicatedSites.DefaultWebAlias; var lastWebAlias = GlobalUtilities.GetLastWebAlias(defaultWebAlias); var defaultPage = urlHelper.Action(routeData.Values["action"].ToString(), routeData.Values["controller"].ToString(), new { webalias = lastWebAlias }); // This ensures that if the page is redirected because of web alias switching, that athe querystring params are passed as well if (currentWebAlias.ToLower() == GlobalSettings.ReplicatedSites.DefaultWebAlias.ToLower()) { // Create new route value dictionary var newList = new RouteValueDictionary(); // Pull in all values that are not the controller,action or webalias foreach (var routeValue in routeData.Values.Where(c => c.Key != "action" && c.Key != "controller" && c.Key != "webalias")) { // Add all values that arent empty to the route data. if (routeValue.Value.ToString().IsNotNullOrEmpty()) { newList.Add(routeValue.Key, routeValue.Value); } } // Grab query in case there are any pieces being sent in with ?example=value var query = Request.Url.Query; //add webalias to the route values. newList.Add("webalias", lastWebAlias); // create new url using new route values and add the query at the end. defaultPage = urlHelper.Action(routeData.Values["action"].ToString(), routeData.Values["controller"].ToString(), newList) + query; } // If we are an orphan and we don't allow them, redirect to a capture page. if (!Settings.AllowOrphans && currentWebAlias.Equals(defaultWebAlias, StringComparison.InvariantCultureIgnoreCase)) { HttpContext.Current.Response.Redirect(urlHelper.Action("webaliasrequired", "error")); } // If we are an orphan, try to redirect the user back to a previously-visited replicated site if (Settings.RememberLastWebAliasVisited && currentWebAlias.Equals(defaultWebAlias, StringComparison.InvariantCultureIgnoreCase) && !defaultWebAlias.Equals(lastWebAlias, StringComparison.InvariantCultureIgnoreCase)) { HttpContext.Current.Response.Redirect(defaultPage); } // Attempt to authenticate the web alias var identityService = new IdentityService(); HttpContext.Current.Items["OwnerWebIdentity"] = identityService.GetIdentity(currentWebAlias); if (HttpContext.Current.Items["OwnerWebIdentity"] != null) { if (Settings.RememberLastWebAliasVisited && currentWebAlias.ToLower() != GlobalSettings.ReplicatedSites.DefaultWebAlias.ToLower()) { GlobalUtilities.SetLastWebAlias(currentWebAlias); } else { GlobalUtilities.DeleteLastWebAlias(); } } else { if (Settings.RememberLastWebAliasVisited) { GlobalUtilities.DeleteLastWebAlias(); lastWebAlias = defaultWebAlias; HttpContext.Current.Response.Redirect(defaultPage); } else { HttpContext.Current.Response.Redirect(urlHelper.Action("invalidwebalias", "error")); } } }
protected void Application_BeginRequest(object sender, EventArgs e) { // Get the route data var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current)); var defaultWebAlias = GlobalSettings.ReplicatedSites.DefaultWebAlias; var identityService = new IdentityService(); // Account for attribute routing and null routeData if (routeData != null && routeData.Values.ContainsKey("MS_DirectRouteMatches")) { routeData = ((List <RouteData>)routeData.Values["MS_DirectRouteMatches"]).First(); } // Added logic specific to soft launch, where only IA enrollment is allowed in the replicated and no web alias should be seen in the URL if (Common.GlobalSettings.Globalization.HideForLive) { HttpContext.Current.Items["OwnerWebIdentity"] = identityService.GetIdentity(defaultWebAlias); return; } // If we have an identity and the current identity matches the web alias in the routes, stop here. var identity = HttpContext.Current.Items["OwnerWebIdentity"] as ReplicatedSiteIdentity; if (routeData == null || routeData.Values["webalias"] == null || (identity != null && identity.WebAlias.Equals(routeData.Values["webalias"].ToString(), StringComparison.InvariantCultureIgnoreCase))) { return; } // Determine some web alias data var urlHelper = new UrlHelper(new RequestContext(new HttpContextWrapper(HttpContext.Current), RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current)))); var currentWebAlias = routeData.Values["webalias"].ToString(); var lastWebAlias = GlobalUtilities.GetLastWebAlias(defaultWebAlias); var defaultPage = urlHelper.Action(routeData.Values["action"].ToString(), routeData.Values["controller"].ToString(), new { webalias = lastWebAlias }); // If we are an orphan and we don't allow them, redirect to a capture page. if (!Settings.AllowOrphans && currentWebAlias.Equals(defaultWebAlias, StringComparison.InvariantCultureIgnoreCase)) { HttpContext.Current.Response.Redirect(urlHelper.Action("webaliasrequired", "error")); } // If we are an orphan, try to redirect the user back to a previously-visited replicated site if (Settings.RememberLastWebAliasVisited && currentWebAlias.Equals(defaultWebAlias, StringComparison.InvariantCultureIgnoreCase) && !defaultWebAlias.Equals(lastWebAlias, StringComparison.InvariantCultureIgnoreCase)) { HttpContext.Current.Response.Redirect(defaultPage); } // Attempt to authenticate the web alias HttpContext.Current.Items["OwnerWebIdentity"] = identityService.GetIdentity(currentWebAlias); if (HttpContext.Current.Items["OwnerWebIdentity"] != null) { if (Settings.RememberLastWebAliasVisited && currentWebAlias.ToLower() != GlobalSettings.ReplicatedSites.DefaultWebAlias.ToLower()) { GlobalUtilities.SetLastWebAlias(currentWebAlias); } else { GlobalUtilities.DeleteLastWebAlias(); } } else { if (Settings.RememberLastWebAliasVisited) { GlobalUtilities.DeleteLastWebAlias(); lastWebAlias = defaultWebAlias; HttpContext.Current.Response.Redirect(defaultPage); } else { HttpContext.Current.Response.Redirect(urlHelper.Action("invalidwebalias", "error")); } } }