From 37e3487eab3bfac2ba45387531d60a11e6a4ba6f Mon Sep 17 00:00:00 2001 From: Rick Strahl Date: Wed, 18 Jun 2014 00:46:53 -0700 Subject: [PATCH] Add RequiredSslAttribute --- Changelog.md | 6 ++ .../Filters/RequireSslAttribute.cs | 79 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 Westwind.Web.Mvc/Filters/RequireSslAttribute.cs diff --git a/Changelog.md b/Changelog.md index 867f7e3..f981596 100644 --- a/Changelog.md +++ b/Changelog.md @@ -50,6 +50,12 @@ and serializes more cleanly. (Note: this affects only JSON output - not inbound JSON parsing. Since formatting differs slightly for some times - namely dictionaries - you might not get two-way fidelity). +* **Add RequireSslAttribute** +Westwind.Web.Mvc +Add RequireSslAttribute that allows to dynamically assign the flag +that decides whether SSL is used. Use a configuration setting, +a static 'delegate' method or an explicit constant bool value. + * **JsonVariables Component** Westwind.Web Component that helps with embedding server side data into client side diff --git a/Westwind.Web.Mvc/Filters/RequireSslAttribute.cs b/Westwind.Web.Mvc/Filters/RequireSslAttribute.cs new file mode 100644 index 0000000..f067cbc --- /dev/null +++ b/Westwind.Web.Mvc/Filters/RequireSslAttribute.cs @@ -0,0 +1,79 @@ +using System; +using System.Configuration; +using System.Reflection; +using System.Web.Mvc; + +namespace Westwind.Web.Mvc +{ + + /// + /// Allows for dynamically assigning the RequireSsl property at runtime + /// either with an explicit boolean constant, a configuration setting, + /// or a Reflection based 'delegate' + /// + public class RequireSslAttribute : RequireHttpsAttribute + { + public bool RequireSsl { get; set; } + + + /// + /// Default constructor forces SSL required + /// + public RequireSslAttribute() + { + // Assign from App specific configuration object + RequireSsl = true; + } + + /// + /// Allows assignment of the SSL status via parameter + /// + /// + public RequireSslAttribute(bool requireSsl) + { + RequireSsl = requireSsl; + } + + /// + /// Allows invoking a method at runtime to check for a + /// value dynamically. + /// + /// Note: The method called must be a static method + /// + /// Fully qualified type name on which the method to call exists + /// Static method on this type to invoke with no parameters + public RequireSslAttribute(Type type, string method) + { + var mi = type.GetMethod(method, BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public); + RequireSsl = (bool)mi.Invoke(type, null); + } + + /// + /// Looks for an appSetting key you specify and if it exists + /// and is set to true or 1 which forces SSL. + /// + /// + public RequireSslAttribute(string appSettingsKey) + { + string key = ConfigurationManager.AppSettings[appSettingsKey] as string; + RequireSsl = false; + if (!string.IsNullOrEmpty(key)) + { + key = key.ToLower(); + if (key == "true" || key == "1") + RequireSsl = true; + } + } + + + public override void OnAuthorization(AuthorizationContext filterContext) + { + if (filterContext != null && + RequireSsl && + !filterContext.HttpContext.Request.IsSecureConnection) + { + HandleNonHttpsRequest(filterContext); + } + } + } +} \ No newline at end of file