public void OnActionExecuting(ActionExecutingContext filterContext) { var maintenanceAttribute = filterContext.ActionDescriptor.FilterDescriptors.Select(x => x.Filter).OfType <MaintenanceAttribute>().FirstOrDefault(); bool disabled = false; if (maintenanceAttribute != null) { disabled = maintenanceAttribute.Disabled; } if (!disabled) { if (_settingProvider == null) { throw new Exception("IMaintenanceSettingProvider must be provided when Disabled is false."); } var startTime = _settingProvider.StartTimeUtc; var endTime = _settingProvider.EndTimeUtc; var warningLead = _settingProvider.WarningLeadTime; var maintenanceWarningMessage = _settingProvider.MaintenanceWarningMessage; bool canBypass = _settingProvider.CanByPass(filterContext.HttpContext.User); var request = filterContext.HttpContext.Request; if (!canBypass && startTime != default(DateTime) && DateTime.UtcNow >= startTime) { if (endTime == default(DateTime) || DateTime.UtcNow <= endTime) { filterContext.Result = new RedirectResult(_settingProvider.GetMaintenanceUrl(request)); } } else if (startTime != default(DateTime) && startTime > DateTime.UtcNow && warningLead > 0) { if (filterContext.HttpContext.Request.Cookies[KeyMaintenanceWarningCookie] == null || filterContext.HttpContext.Request.Cookies[KeyMaintenanceWarningCookie] != "1") { var difference = (startTime - DateTime.UtcNow); if (difference.TotalSeconds < warningLead) { //string baseUrl = string.Format("{0}://{1}{2}", filterContext.HttpContext.Request.Scheme, filterContext.HttpContext.Request.Host, filterContext.HttpContext.Request.PathBase); // string script = @"$.ajax({{ //url: '{0}/api/MaintenanceFilter/MarkShowed', //method: ""post"" // }}); "; _sessionMessageManager.SetMessage(MessageType.Warning, MessageBehaviors.Modal, maintenanceWarningMessage, KeyMaintenanceWarningMessage);//, string.Format(script, baseUrl)); filterContext.HttpContext.Response.Cookies.Append(KeyMaintenanceWarningCookie, "1", new CookieOptions { HttpOnly = true }); } } } } }
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext filterContext) { if (!Disabled) { if (!Disabled) { if (_settingProvider == null) { throw new Exception("IMaintenanceSettingProvider must be provided when Disabled is false."); } var startTime = _settingProvider.StartTimeUtc; var endTime = _settingProvider.EndTimeUtc; var warningLead = _settingProvider.WarningLeadTime; var maintenanceWarningMessage = _settingProvider.MaintenanceWarningMessage; bool canBypass = _settingProvider.CanByPass(HttpContext.Current.User); var requestUrl = filterContext.Request.RequestUri; if (!canBypass && startTime != default(DateTime) && DateTime.UtcNow >= startTime) { if (endTime == default(DateTime) || DateTime.UtcNow <= endTime) { filterContext.Response = new HttpResponseMessage(HttpStatusCode.OK); //response doesn't work string fullyQualifiedUrl = _settingProvider.GetMaintenanceUrl(requestUrl); //response.Headers.Location = new Uri(fullyQualifiedUrl); filterContext.Response.Headers.Add("FORCE_REDIRECT", fullyQualifiedUrl); return; } } if (startTime != default(DateTime) && startTime > DateTime.UtcNow && warningLead > 0) { CookieHeaderValue cookie = filterContext.Request.Headers.GetCookies(KeyMaintenanceWarningCookie).FirstOrDefault(); if (cookie == null || cookie[KeyMaintenanceWarningCookie].Value != "1") { var difference = (startTime - DateTime.UtcNow); if (difference.TotalSeconds < warningLead) { SessionMessageManager.SetMessage(MessageType.Warning, MessageBehaviors.Modal, maintenanceWarningMessage, KeyMaintenanceWarningMessage); CookieHeaderValue newCookie = new CookieHeaderValue(KeyMaintenanceWarningCookie, "1"); newCookie.HttpOnly = true; filterContext.Response.Headers.AddCookies(new CookieHeaderValue[] { newCookie }); } } } } base.OnActionExecuting(filterContext); } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!Disabled) { if (_settingProvider == null) { throw new Exception("IMaintenanceSettingProvider must be provided when Disabled is false."); } var startTime = _settingProvider.StartTimeUtc; var endTime = _settingProvider.EndTimeUtc; var warningLead = _settingProvider.WarningLeadTime; var maintenanceWarningMessage = _settingProvider.MaintenanceWarningMessage; bool canBypass = _settingProvider.CanByPass(filterContext.HttpContext.User); var requestUrl = filterContext.HttpContext.Request.Url; if (!canBypass && startTime != default(DateTime) && DateTime.UtcNow >= startTime) { if (endTime == default(DateTime) || DateTime.UtcNow <= endTime) { filterContext.Result = new RedirectResult(_settingProvider.GetMaintenanceUrl(requestUrl)); } } else if (startTime != default(DateTime) && startTime > DateTime.UtcNow && warningLead > 0) { if (filterContext.HttpContext.Request.Cookies[KeyMaintenanceWarningCookie] == null || filterContext.HttpContext.Request.Cookies[KeyMaintenanceWarningCookie].Value != "1") { var difference = (startTime - DateTime.UtcNow); if (difference.TotalSeconds < warningLead) { SessionMessageManager.SetMessage(MessageType.Warning, MessageBehaviors.Modal, maintenanceWarningMessage, KeyMaintenanceWarningMessage); HttpCookie cookie = new HttpCookie(KeyMaintenanceWarningCookie); cookie.Value = "1"; cookie.HttpOnly = true; filterContext.HttpContext.Response.Cookies.Add(cookie); } } } } base.OnActionExecuting(filterContext); }