RequireHttps Attribute
Hey All,
Does anyone know if the RequireHttps attribute behavior changed? I have a web site I converted from RC1 where the RequireHttps attribute worked as expected. But now when I hit a controller adorned with RequireHttps it does not redirect me to the page under SSL instead I get a page cannot be found error. Any ideas?
Thanks!
What is always fun is that the code is available!
Translating the code in English gives us that:
If the request is not a GET, returns a 403 forbidden. If a specific port is specified, use that port. Rewrite the url with https:// and reusing every component of the called URL.
Thanks for the code link. Will explore more. In the meantime I have reproduced the issue by doing the following steps:
- Create a new ASP NET Core Web Application project targeting the .NET Framework. Pick Web Application so that you have the basic implementation to work with.
- On the HomeController add the [RequireHttps] attribute to the About ActionMethod
- Run the project and click on the About menu option
In development this should fail to find the page because RequireHttps code does not know how to apply the right development port number (e.g. 44343). This can be easily fixed by building your own version of the RequireHttps attribute. Thanks to MaximRouiller above. However this should not fail when deployed to an Azure website. So I published the web site to an Azure WebApp. When I clicked on the About menu I get the same error.
Let me know if anyone tries this if they have a different result. Am I doing something wrong? RC1 seemed to work correctly.
It seems you're right something wrong, when I follow your steps. To get to the about page I had to manually add the port to the url. Maybe an issue to get the port number and use it within the require https attribute.
reading more closely the code is selecting the port through the options accessor, I was able to get rid of the error by adding this to the configureservices part of the startup class.
services.AddMvc(o => o.SslPort = 44300);
but then all the app is browsed through this port, I don't know if this will do it in your case.
Ok thanks. Will give that a try...
Still seems broken. We added something like this:
#if DEBUG
services.AddMvc(options => { options.SslPort = 44342; });
#else
services.AddMvc(options => { options.SslPort = 443; });
#endif
and places RequireHttps attribute on our controller. When we hit the site via http it does not transfer the site over to https. This seemed to work fine under RC1 so either the methodology has changed or something is broken.
For now we are enforcing https via the web.config per a url rewrite rule which seems like a bad hack.
maybe try with environment variables, add an environment property you can access in the configureservices method. Something like this may do it.
public Startup(IHostingEnvironment env)
{
...
Environment = env;
}
private IHostingEnvironment Environment { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.SslPort = Environment.IsDevelopment() ? 44342 : 443;
});
}
Ok, tried that as well. It works fine in development but when deployed to an Azure website I get the same problem as stated above. Page cannot be displayed error. I tried restarting the WebApp in azure, etc. but no luck. To repro this do the following:
- Create a new ASP NET Core Web Application project targeting the .NET Framework. Pick Web Application so that you have the basic implementation to work with.
- On the HomeController add the [RequireHttps] attribute to the HomeController class
- Add code suggested above by @Pictuel to have the SSL port switch based on environment
- Run the project
In development this now works as the code should switch the port to the correct development SSL port.
- Deploy to an Azure WebApp. Make sure deploy settings are set to "Release"
What we are seeing is the This page can't be displayed error. We made sure the url was https but still no luck.
Let us know if anyone can confirm.
Much appreciated!
This issue is being closed because it has not been updated in 3 months.
We apologize if this causes any inconvenience. We ask that if you are still encountering this issue, please log a new issue with updated information and we will investigate.