在非 https 网站上强制执行 https 请求 - chrome 中出现混合内容错误

enforce https request on a non https website - Mixed Content error in chrome

这可能是一个奇怪的案例,如果有更好的方法,请随时告诉我。

所以我有一个带有负载均衡器的应用程序。负载均衡器只接受 HTTPs 流量。到目前为止一切顺利。

负载均衡器然后将它作为 HTTP 发送到我的 asp.netcore。

含义:

但这给了我这个问题:

Mixed Content: The page at 'pagePath' was loaded over HTTPS, but requested an insecure manifest 'path/site.webmanifest'. This request has been blocked; the content must be served over HTTPS.

这是我的创业公司

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseHttpsRedirection();
    }
    else
    {
        app.UseHsts();
    }

    app.UseStaticFiles();
    app.UseCookiePolicy();

    app.UseAuthentication();

    app.UseExceptionHandler("/error/500");
    app.UseMiddleware<WebRequestLoggerMiddleWare>();
    app.UseMiddleware<UserTimeZoneMiddleWare>();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

我的 cookie 政策还在 statup.cs

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.LoginPath = new PathString("/account/login/");
        options.AccessDeniedPath = new PathString("/account/forbidden/");
        options.SlidingExpiration = true;
        options.ExpireTimeSpan = TimeSpan.FromHours(cookieSettings.CookieExpirationInHours);
        options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
        options.Cookie.HttpOnly = true;

    });

确保通过 HTTPS 请求网络清单的最佳方法是什么?

我是否应该强制整个网站为 HTTPS 并将来自 LB 的流量路由为 https?这是一种方式,但没有必要...

您的反向代理和您的 ASP.NET 核心应用程序之间的流量不必通过 https 加密,但必须满足两个条件,以便您的 ASP.NET 核心应用程序知道原始HTTP 请求来自 https 或 http。

  1. 您的反向代理必须使用原始主机的原始协议和 IP 发送正确的 headers。默认的 headers 是远程 IP 的 X-Forwarded-For 和协议的 X-Forwarded-Proto
  2. 在 IIS 上 运行 时默认设置转发的 headers 中间件,并且(根据文档)需要为其他托管方案启用。

可以在 Configure ASP.NET Core to work with proxy servers and load balancers 下阅读文档。

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.Configure<ForwardedHeadersOptions>(options =>
    {
        options.ForwardedHeaders = 
            ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseForwardedHeaders();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();
    // In ASP.NET Core 1.x, replace the following line with: app.UseIdentity();
    app.UseAuthentication();
    app.UseMvc();
}

如果您的代理名称 headers 不同于 X-Forwarded-ForX-Forwarded-ProtoX-Forwarded-Host,您可以使用 ForwardedForHeaderNameForwardedProtoHeaderNameForwardedHostHeaderName ForwardedHeadersOptions 上的属性来更改它。

此外,您的 Startup class 应如下所示

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    // redirect to Https
    app.UseHttpsRedirection();

根据官方示例并确保用户在 his/her 首次访问时被重定向到 http,HSTS 应该处理连续的请求