代码之家  ›  专栏  ›  技术社区  ›  MorgoZ

无法使用HTTP进行开发,总是被重定向到HTTPS

  •  0
  • MorgoZ  · 技术社区  · 6 年前

    services.AddHttpsRedirection(options =>
            {
                options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
                options.HttpsPort = 443;
            });
    

    启动时。配置:

    app.UseHttpsRedirection();
    

    public static void ConfigureKestrelServerOptions(this KestrelServerOptions options)
        {
            var configurationService = options.ApplicationServices.GetRequiredService<IConfiguration>();
            var environmentService = options.ApplicationServices.GetRequiredService<IHostingEnvironment>();
    
            var endpoints = configurationService.GetSection("HttpServer:Endpoints")
                .GetChildren()
                .ToDictionary(section => section.Key, section =>
                {
                    var endpoint = new EndPointSettings();
                    section.Bind(endpoint);
                    return endpoint;
                });
    
            foreach (var endpoint in endpoints)
            {
                var config = endpoint.Value;
                var port = config.Port ?? (config.Scheme == "https" ? 443 : 8080);
    
                var ipAddresses = new List<IPAddress>();
                if (config.Host == "localhost")
                {
                    ipAddresses.Add(IPAddress.IPv6Loopback);
                    ipAddresses.Add(IPAddress.Loopback);
                }
                else if (IPAddress.TryParse(config.Host, out var address))
                {
                    ipAddresses.Add(address);
                }
                else
                {
                    ipAddresses.Add(IPAddress.IPv6Any);
                }
    
                foreach (var address in ipAddresses)
                {
                    options.Listen(address, port,
                        listenOptions =>
                        {
                            if (config.Scheme == "https")
                            {
                                var certificate = LoadCertificate(config, environmentService);
                                listenOptions.UseHttps(certificate);
                            }
                        });
                }
            }
        }
    
    WebHost.CreateDefaultBuilder(args)
    .UseKestrel(options => options.ConfigureKestrelServerOptions())
    

    之后,我在Chrome上添加了一个X509证书,所有https都工作正常。

    " http ://本地主机:8080“ " https ://localhost/Account/LogIn?返回URL=%2F” .

    有什么帮助吗?谢谢

    1 回复  |  直到 6 年前
        1
  •  0
  •   Arsiwaldi    6 年前

    在此配置中,您将强制所有HTTP请求在HTTPS上重定向。如果您希望能够同时使用这两种配置,那么使用这种配置就足够了(在开发方法中也足够了):

    WebHost.CreateDefaultBuilder(args)
                    .UseKestrel(options =>
                    {
                        options.Listen(IPAddress.Loopback, 5000); 
                        options.Listen(IPAddress.Loopback, 5001, listenOptions => { listenOptions.UseHttps(); });
                    })
                    .UseStartup<Startup>();
    

    如果运行此配置,我显然能够访问两个端点。

    http://localhost:5000 能够侦听所有HTTP请求。
    https://localhost:5001 能够侦听所有HTTPS请求。

    但是,它需要运行 Kestrel 无反向代理 IIS ,因为它会覆盖当前配置中的集合终结点。若在Visual Studio中运行该程序,则调试模式下WebApplication的属性应设置为启用 发射 应该是 Project .

    问题是,即使使用上面的注释行,您也无法运行HTTP,这可能是因为HSTS是高度可缓存的 . 我怀疑浏览器,缓存中的某个地方保存了一些信息,关于客户端和服务器之间的通信应该如何工作。因此,它可能会导致重定向,甚至配置被设置。