ASP.NET 核心服务不会将 http:localhost:<https-port> 重定向到 https 架构
ASP.NET Core service won't redirect http:localhost:<https-port> to https schema
我在 https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-2.2&tabs=visual-studio 阅读了说明,这是我的 Startup.cs
代码(样板生成的代码):
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(...);
}
}
我的launchSettings.json
:
{
"profiles": {
"MyProject": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
当我打开 http://localhost:5000
时,我被重定向到 https://localhost:5001
。但是,当我打开 http://localhost:5001
时,我只是收到一个错误页面。我想重定向到 https
。我在文档中遗漏了什么吗?
简短的回答是,你不能那样做。
当您尝试访问 http://localhost:5001
时,您最终会遇到双方试图以不同 "language" 进行通信的情况(服务器需要 TLS 握手,但却得到了一些垃圾 - 纯文本 http 请求) .
因此,服务器无法重定向你,因为双方都无法建立沟通渠道。
如果您尝试使用 http 连接到端口 443,有些浏览器足够智能,可以切换到 https,但那是客户端。在同一个端口上托管 https 和 http 是不可能的。
我在 https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-2.2&tabs=visual-studio 阅读了说明,这是我的 Startup.cs
代码(样板生成的代码):
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(...);
}
}
我的launchSettings.json
:
{
"profiles": {
"MyProject": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
当我打开 http://localhost:5000
时,我被重定向到 https://localhost:5001
。但是,当我打开 http://localhost:5001
时,我只是收到一个错误页面。我想重定向到 https
。我在文档中遗漏了什么吗?
简短的回答是,你不能那样做。
当您尝试访问 http://localhost:5001
时,您最终会遇到双方试图以不同 "language" 进行通信的情况(服务器需要 TLS 握手,但却得到了一些垃圾 - 纯文本 http 请求) .
因此,服务器无法重定向你,因为双方都无法建立沟通渠道。
如果您尝试使用 http 连接到端口 443,有些浏览器足够智能,可以切换到 https,但那是客户端。在同一个端口上托管 https 和 http 是不可能的。