Requirehttps for DEVELOPMENT and LIVE websites

491 views Asked by At

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?

2

There are 2 answers

0
Jasen On BEST ANSWER

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
{
    ...
}
0
bdemarzo On

If you switch between debug/release builds, you can do this in your code:

#if !DEBUG
    [RequireHttps]
#endif