.netasp.net-mvcsslrequirehttps

Requirehttps for DEVELOPMENT and LIVE websites


I have some ASP .NET MVC4 website which I have to put first to DEVELOPMENT place to test. And development website doesn't have SSL but the LIVE has it.

Well. I have to use [Requirehttps] inside of 20 controllers and I cannot comment it every time when I have test it under DEV website.

Is there any approach to configure [Requirehttps] based at least on some web.config settings or perhaps there is another approach? I mean I don't like to comment around 20 [Requirehttps] each time when I have publish website under LIVE.

Any clue?


Solution

  • Override the RequireHttpsAttribute

    web.config

    <appSettings>
      <add key="SSL" value="false" />
    </appSettings>
    

    MyHttpsAttribute.cs

    public class MyHttpsAttribute : RequireHttpsAttribute, IAuthorizationFilter
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (bool.Parse(AppSettings["SSL"]) != true) {
                return;
            }
            base.OnAuthorization(filterContext);
        }
    }
    

    BaseController

    [MyHttps]
    public abstract class BaseController : Controller
    {
        ...
    }