KES

Forcing to ssl/https in a controller action result is quite simple. All you need to do is add the [RequireHttps] attribute to the controller action. If you want to make an entire controller or an entire site do this it can become tedious.
Create a new custom attribute. Mark its target to Class and inheritable.


//#define ssl
using System;
using System.Web.Mvc;
namespace MyNameSpace.Controllers
{
    [AttributeUsage(AttributeTargets.Class, Inherited = true)]
    internal sealed class InheritableAttribute : Attribute { }

Now create a new class that extends Controller. Mark it Inheritable using that new attribute created above. This class is empty and its only purpose is to redirect all controller actions to https. Where this becomes handy is during testing on a local development server without ssl. Here I’m using a compiler directive exactly for this purpose.

   
[Inheritable]
#if ssl
    [RequireHttps]
#endif
    public class ControllerBaseController : Controller {}
}

To apply this to any or all controllers just do the following:


// dummy controler for dem
// inherit from your newly created BaseController just don’t try and name it BaseControler 
    public class CheckOutController : ControllerBaseController
    {
// …action 1,2,3. …
    }

I hope this helps some one
KES

Leave a Reply