I have a site which has to be secure - is there a downside to having the [RequireHttps] attribute on the home controller?
To answer your Specific question, there are only three downsides I can think of if you require Https on your HomeController.
There is an insignificant increase in server CPU per request. (If this application is served to millions/billions of users this may not be insignificant over a specific time)
There is an insignificant increase in network activity per request. (this also applies to number of requests over a specific time)
If your certificate ever expires, users will get a negative experience of your Home pages.
But if you want to add the Https Attribute to your whole site there are 2 ways you can do that.
Application_BeginRequest method.Conditional Filters. You can add the [Https] attibute to every Action method, to a specific Controller or any other conditionRedirect in the Application_BeginRequest method.
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if ( !Request.IsSecureConnection)
{
string path = string.Format("https{0}", Request.Url.AbsoluteUri.Substring(4));
Response.Redirect(path);
}
}
Using Conditional Filters
Add this class to your project
public class ConditionalFilterProvider : IFilterProvider
{
private readonly
IEnumerable<Func<ControllerContext, ActionDescriptor, object>> _conditions;
public ConditionalFilterProvider(
IEnumerable<Func<ControllerContext, ActionDescriptor, object>> conditions)
{
_conditions = conditions;
}
public IEnumerable<Filter> GetFilters(
ControllerContext controllerContext,
ActionDescriptor actionDescriptor)
{
return from condition in _conditions
select condition(controllerContext, actionDescriptor) into filter
where filter != null
select new Filter(filter, FilterScope.Global, null);
}
}
Change your global.asax RegisterGlobalFilters method to add the [Https] attribute to every ActionMethod
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
IEnumerable<Func<ControllerContext, ActionDescriptor, object>> conditions =
new Func<ControllerContext, ActionDescriptor, object>[] {
(c, a) => new RequireHttpsAttribute()
};
var provider = new ConditionalFilterProvider(conditions);
FilterProviders.Providers.Add(provider);
filters.Add(new HandleErrorAttribute());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With