如何从 ASP.NET Core 中的 HTTPS 重定向中排除路径?

How to exclude path from HTTPS-redirection in ASP.NET Core?

我有一个问题: 我有 ASP.NET Core 在端口 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。 据我所知 google,这应该是这样的:

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 

但是如果我这样做,我会得到:

No webpage was found for the web address: https://localhost:44322/
HTTP ERROR 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 个问题:

  • 首先:MapWhen终止管道
    因此,如果您希望管道在此之后继续,您必须改用 app.UseWhen
  • 其二:StartsWithSegments的参数不能以/结尾,所以是
    StartsWithSegments("/.well-known/acme-challenge");