【问题标题】:HTTPS not working in ASP.NET Core app hosted in CaproverHTTPS 在 Caprover 中托管的 ASP.NET Core 应用程序中不起作用
【发布时间】:2022-02-22 10:17:14
【问题描述】:

我在 Linux VM 中的开源 Caprover Paas 中托管了一个 ASP.NET Core Docker Web 应用程序,它运行良好。使用 Caprover 界面,我能够配置站点的 LetsEncrypt SSL 证书,并且浏览器显示一个挂锁并说连接是安全的。问题是 ASP.NET 没有检测到应用程序在 Https 模式下运行,并且Request.IsHttps 始终为假。这是我的测试。

index.cshtml

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

 @if (Request.IsHttps)
{
    <div class="alert alert-success"><strong>HTTPS:</strong> You are using a secure connection</div>
}
else
{
    <div class="alert alert-warning"><strong>HTTP:</strong> Your connection is NOT secure</div>
}

它总是显示

Caprover 使用 Docker 容器和 Nginx 代理服务器,所以我怀疑这是问题,因为Request.IsHttps 在我的 Windows 笔记本电脑上运行应用程序时返回 true。

这是program.cs,只是一个典型的Visual Studio模板。

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    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.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();

问题是如何配置应用程序以检测它是否在 Https 中运行?

【问题讨论】:

    标签: asp.net-core nginx caprover


    【解决方案1】:

    正如怀疑的那样,问题是 Nginx 代理服务器处理 https 连接的加密/解密,但是在将请求转发到容器时,它使用纯 http。所以 asp.net 永远不会看到 https 连接,因为它在代理服务器处终止。解决方案是转发X-Forwarded-ForX-Forwarded-Proto 标头。请参阅以下修改。

    Program.cs

    using Microsoft.AspNetCore.HttpOverrides;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container.
    builder.Services.AddRazorPages();
    
    //ADD: Configure middleware to add X-Forwarded-For and X-Forwarded-Proto headers
    builder.Services.Configure<ForwardedHeadersOptions>(options =>
    {
        options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
        //accept all networks and proxies
        options.KnownNetworks.Clear();
        options.KnownProxies.Clear();
    });
    
    var app = builder.Build();
    
    //ADD: use ForwardedHeaders middleware
    app.UseForwardedHeaders();
    
    
    // Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment())
    {
        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();
    }
    
    //REMOVE: not needed as nginx proxy server handling https
    //app.UseHttpsRedirection();
    
    app.UseStaticFiles();
    
    app.UseRouting();
    
    app.UseAuthorization();
    
    app.MapRazorPages();
    
    app.Run();
    

    这里是complete sample on Github

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 2017-03-25
      • 1970-01-01
      • 2019-11-20
      • 1970-01-01
      相关资源
      最近更新 更多