目的:
使用.netcore自带的kestrel加载自己指定的ssl
实验版本
- aspnet core 2.2
- aspnet core 3.1
首先准备ssl文件:(密码为:123456)
代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace server
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("https://*:5001")
.UseKestrel(option =>
{
option.ConfigureHttpsDefaults(i =>
{
i.ServerCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("./ssl.pfx", "123456");
});
}).UseStartup<Startup>();
}
}