У меня проблема: У меня ASP.NET Core прослушивает HTTPS через порт 44322 и HTTP через порт 51851.
{
"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.
Из того, что я могу гуглить, это должно выглядеть так:
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
Но если я сделаю это так, я получу:
Веб-страница не найдена для веб-адреса: 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");