Reputation: 9095
I read the instructions at https://learn.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-2.2&tabs=visual-studio , and this is my Startup.cs
code (which is the boilerplate generated code):
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// ...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseMvc(...);
app.UseSpa(...);
}
}
My launchSettings.json
:
{
"profiles": {
"MyProject": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
When I open http://localhost:5000
I'm redirected to https://localhost:5001
. However, when I open http://localhost:5001
I'm just getting an error page. I would want to get redirected to https
. Am I missing something in the documentation?
Upvotes: 2
Views: 2904
Reputation: 1662
The short answer is, you cannot do that.
When you try to access http://localhost:5001
what happens is you end up with two sides trying to communicate in different "language" (server expects TLS handshake, but instead gets some garbage - plaintext http request).
Therefore, the server can't redirect you, because there's no communication channel both sides undestand established.
Some browsers are smart enough to switch over to https if you're trying to connect to port 443 using http, but that's client side. It's impossible to host https and http on the same port.
Upvotes: 4