【问题标题】:Using Kestrel https .net core 6使用 Kestrel https .net core 6
【发布时间】:2021-12-18 09:27:26
【问题描述】:

我在 .net core 5 中有应用程序。 这是 Startup.cs 中的代码

'''''

public static IHostBuilder CreateHostBuilder(string[] args) =>
        //Host.CreateDefaultBuilder(args)
        //    .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
    
        Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder => {
        webBuilder
            .UseStartup<Startup>()
            .UseKestrel(o =>
            {
                o.Listen(IPAddress.Any, 443, opt =>
                {
                    opt.UseHttps("pathfto.pfx", "passwordtocert");
                });
            });
    });

我想把它升级到 .net core 6

我以为会是这样的

var builder = WebApplication.CreateBuilder(args);

builder.Host
.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder
        .UseKestrel(o =>
        {
            o.Listen(IPAddress.Any, 443, opt => { opt.UseHttps("pathto.pfx", "passwordtocert"); });
        });
});

但是当我尝试编译它时它不起作用。

提前感谢您提供任何解决方案。

【问题讨论】:

标签: c# .net https kestrel asp.net-core-6.0


【解决方案1】:

尝试使用builder.WebHost

builder.WebHost.ConfigureKestrel(options =>
{
    options.Listen(IPAddress.Any, int.Parse(builder.Configuration.GetSection("SSL")["port"]), listenOptions =>
    {
        listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
        if (builder.Configuration.GetSection("SSL")["sertificateName"].Trim() != "")
            listenOptions.UseHttps(Path.Combine(AppContext.BaseDirectory, "cfg", builder.Configuration.GetSection("SSL")["sertificateName"]), builder.Configuration.GetSection("SSL")["password"]);

    });
});

更多详情请访问https://docs.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 2019-10-02
    • 2018-03-02
    • 2019-10-10
    相关资源
    最近更新 更多