![](/img/trans.png)
[英]ASP.NET Core 2.1 no HTTP/HTTPS redirection in App Engine
[英]How to exclude path from HTTPS-redirection in ASP.NET Core?
我遇到了問題:我有 ASP.NET 核心在端口 44322 上監聽 HTTPS,在端口 51851 上監聽 HTTP。
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:51851",
"sslPort": 44322
}
},
現在我想添加 HTTPS 重定向,但僅適用於以下所有內容:
http://localhost:51851/.well-known/acme-challenge/*
例如http://localhost:51851/.well-known/acme-challenge/token.txt
不應重定向到 HTTPS。 從我可以谷歌,這應該 go 像這樣:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace TestApplicationHttps
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpsRedirection(options =>
{
if(System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
// options.HttpsPort = 443;
options.HttpsPort = 44322;
else
options.HttpsPort = 5005;
});
services.AddRazorPages();
} // End Sub ConfigureServices
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedFor
| Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto
});
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.MapWhen(
delegate(Microsoft.AspNetCore.Http.HttpContext httpContext)
{
// http://localhost:51851/.well-known/acme-challenge/token.txt
// http://localhost:51851/Privacy
bool b = !httpContext.Request.Path.StartsWithSegments("/.well-known/acme-challenge/");
return b;
}
,
delegate (IApplicationBuilder appBuilder)
{
appBuilder.UseHttpsRedirection();
}
);
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
} // End Sub Configure
} // End Class Startup
} // End Namespace TestApplicationHttps
但如果我這樣做,我會得到:
沒有找到web地址的網頁:https://localhost:44322/
HTTP 錯誤 404
另一方面,如果我將其更改為
bool b = httpContext.Request.Path.StartsWithSegments("/.well-known/acme-challenge/");
如
app.MapWhen(
delegate(Microsoft.AspNetCore.Http.HttpContext httpContext)
{
// http://localhost:51851/.well-known/acme-challenge/token.txt
// http://localhost:51851/Privacy
bool b = httpContext.Request.Path.StartsWithSegments("/.well-known/acme-challenge/");
return b;
}
,
delegate (IApplicationBuilder appBuilder)
{
// appBuilder.UseHttpsRedirection();
appBuilder.UseStaticFiles();
appBuilder.UseRouting();
appBuilder.UseAuthorization();
appBuilder.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
);
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
然后它只是不斷將 http 重定向到 https 無處不在......
如何從 HTTPS 重定向中排除該路徑?
啊沒關系。
有2個問題:
StartsWithSegments("/.well-known/acme-challenge");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.