using System;
using System.Web;
using System.Web.Mvc;
namespace Infrastructure
{
/// <summary>
/// MVC attribute that redirects non HTTPS requests to a HTTPS URI.
/// </summary>
public class RedirectToHTTPSAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase req = filterContext.HttpContext.Request;
HttpResponseBase res = filterContext.HttpContext.Response;
//Check if require is HTTPS
if (!req.IsSecureConnection)
{
var builder = new UriBuilder(req.Url)
{
Scheme = Uri.UriSchemeHttps,
Port = 443
};
res.Redirect(builder.Uri.ToString());
}
base.OnActionExecuting(filterContext);
}
}
}