Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable ssl for all controllers in mvc application

I have a MVC 5 app and I installed the ssl certificate and I'm now connecting with https, but in my code I had to set the '[requirehttps]' attribute on the homecontroller like so:

[RequireHttps]
public class HomeController : Controller
{}

Isn't there a way to set it for the whole application so I don't have to do this for each and every controller I have in the app?

like image 647
user1186050 Avatar asked Jun 19 '14 23:06

user1186050


People also ask

How do I enable SSL Certificate in Visual Studio?

How to Enable SSL in Visual Studio Development Server? In the Solution Explorer click on the WebAPIEnableHTTP Web API project and press F4 key on the keyboard which will open the Project Properties window. From the Project Properties window, we need to set the SSL Enabled property to true.

What is SSL MVC?

Secure Sockets Layer (SSL) is the standard security technology for establishing an encrypted link between a web server and a browser.


2 Answers

The [RequireHttps] attribute is inherited, so you could create a base controller, apply the attribute to that, and then derive all your controllers from that base.

[RequireHttps]
public abstract class BaseController : Controller
{}

public class HomeController : BaseController
{}

public class FooController : BaseController
{}
like image 90
Andrew Cooper Avatar answered Sep 28 '22 09:09

Andrew Cooper


Use the RegisterGlobalFilters method in your FiltersConfig.

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new RequireHttpsAttribute());
    }
}
like image 31
Rowan Freeman Avatar answered Sep 28 '22 07:09

Rowan Freeman