Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Commit

Permalink
Add RequiredSslAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
RickStrahl committed Jun 18, 2014
1 parent bb68dee commit 37e3487
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
<small>Westwind.Web.Mvc</small>
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**
<small>Westwind.Web</small>
Component that helps with embedding server side data into client side
Expand Down
79 changes: 79 additions & 0 deletions Westwind.Web.Mvc/Filters/RequireSslAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Configuration;
using System.Reflection;
using System.Web.Mvc;

namespace Westwind.Web.Mvc
{

/// <summary>
/// Allows for dynamically assigning the RequireSsl property at runtime
/// either with an explicit boolean constant, a configuration setting,
/// or a Reflection based 'delegate'
/// </summary>
public class RequireSslAttribute : RequireHttpsAttribute
{
public bool RequireSsl { get; set; }


/// <summary>
/// Default constructor forces SSL required
/// </summary>
public RequireSslAttribute()
{
// Assign from App specific configuration object
RequireSsl = true;
}

/// <summary>
/// Allows assignment of the SSL status via parameter
/// </summary>
/// <param name="requireSsl"></param>
public RequireSslAttribute(bool requireSsl)
{
RequireSsl = requireSsl;
}

/// <summary>
/// Allows invoking a method at runtime to check for a
/// value dynamically.
///
/// Note: The method called must be a static method
/// </summary>
/// <param name="typeName">Fully qualified type name on which the method to call exists</param>
/// <param name="method">Static method on this type to invoke with no parameters</param>
public RequireSslAttribute(Type type, string method)
{
var mi = type.GetMethod(method, BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public);
RequireSsl = (bool)mi.Invoke(type, null);
}

/// <summary>
/// Looks for an appSetting key you specify and if it exists
/// and is set to true or 1 which forces SSL.
/// </summary>
/// <param name="appSettingsKey"></param>
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);
}
}
}
}

0 comments on commit 37e3487

Please sign in to comment.