解决 IIS 服务器上 .NET 应用程序的 HTTPS 问题

问题描述 投票:0回答:1

我有一个 IIS 服务器,其中包含所有已安装的捆绑包和 co ... 如果我在本地运行构建 https 有效,而在服务器上则没有任何内容。通过 PortScanner,我看到它打开了端口,EventManger 向我显示进程已启动。 通过 http,我可以看到所有内容,它可以在那里工作,但不能在 https 上工作。

我在同一个 IIS 上有相同的 ssl 证书,前端使用 vue3,它工作正常...但这里不是 .net 应用程序...我的错误到底在哪里?

程序.cs

using BaseBackend.Controllers;

using Microsoft.AspNetCore.Authentication.Cookies;


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews(); // For MVC support
builder.Services.AddControllers(); // For API support

// Configure Swagger/OpenAPI
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Register IConfiguration for DI
//builder.Services.AddSingleton<IConfiguration>(builder.Configuration);

// Register TuningDatabaseHandler for DI
builder.Services.AddScoped<TuningDatabaseHandler>();
builder.Services.AddScoped<MyUploadedFileHandler>();
builder.Services.AddScoped<StringReplacementHandler>();
builder.Services.AddScoped<TaskHandler>();
builder.Services.AddScoped<EcuHandler>();



// Configure Authentication
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.LoginPath = "/api/login";
        options.LogoutPath = "/api/logout";
        options.Cookie.HttpOnly = true;

    });

// Configure Authorization
builder.Services.AddAuthorization();

// Configure CORS
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowFrontend", builder =>
    {
        builder.WithOrigins("https://localhost")
               .AllowAnyHeader()
               .AllowAnyMethod()
               .AllowCredentials();

    });
});

builder.Services.AddHttpsRedirection(options =>
{
    options.HttpsPort = 44390; // Specify the HTTPS port where is same in IIS
});

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();

// Enable CORS policy
app.UseCors("AllowFrontend");

//app.UseHttpsRedirection();

// Use authentication and authorization middleware
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.MapGet("/", () => "Test Page: Application is Running!");
app.Run();

应用程序设置.json

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
    },
      "AllowedHosts": "*",
      "Swagger": {
        "Enabled": true
      },
      "ConnectionStrings": {
        "dbo": "Server=.\\SQLEXPRESS;Integrated Security=True;Database=dbo;"
      },
      "Kestrel": {
        "Endpoints": {
            "Https": {
                "Url": "https://localhost:44390" // Set HTTPS to listen on port 44390
          }
        }
    }
}
c# .net http iis https
1个回答
0
投票

我刚刚创建了基本的 ASP.NET Core MVC 项目并将其部署到 IIS,并且可以通过您提到的端口查看它,并且无需任何修改。像这样:

enter image description here 另外,我发现你有一些与

Kestrel
相关的设置,我不知道你在哪里使用了它。据我所知,可能出了什么问题。在文档中,它说:
In production, a TLS certificate must be explicitly configured. At a minimum, a default certificate must be provided.

如果你想在代码中配置它,应该是这样的:

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureKestrel((context, serverOptions) =>
{
    serverOptions.Listen(IPAddress.Loopback, 5000);
    serverOptions.Listen(IPAddress.Loopback, 44390, listenOptions =>
    {
        listenOptions.UseHttps("yourcert.pfx", "yourassword");
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.