Home  >  Article  >  Backend Development  >  Using HTTPS (SSL) in ASP.NET Core Kestrel

Using HTTPS (SSL) in ASP.NET Core Kestrel

高洛峰
高洛峰Original
2017-02-07 11:52:051292browse

In ASP.NET Core, if you want to use HTTPS for encrypted transmission of the site in Kestrel, you can follow the following method

Apply for a certificate

This step is I won’t go into details, there are free and paid ones, and after the application is completed, you will be given a file ending in *.pfx.

Add NuGet package

Find in nuget and then add a reference to Microsoft.AspNetCore.Server.Kestrel.Https in the program

Configuration

Copy the file ending with *.pfx to the program's Web root directory, and then modify the Programs.cs file:

public class Program
{
 public static void Main(string[] args) {
  var config = new ConfigurationBuilder().AddCommandLine(args).AddEnvironmentVariables("ASPNETCORE_").Build();
 
  var host =
   new WebHostBuilder().UseConfiguration(config).UseKestrel(ConfigHttps()).UseContentRoot(
    Directory.GetCurrentDirectory()).UseIISIntegration().UseStartup().Build();
   
  host.Run();
 }
 
 private static Action ConfigHttps() {
  return x => {
   var pfxFile = Path.Combine(Directory.GetCurrentDirectory(), "*.pfx");
   //password 填写申请的密钥
   var certificate = new X509Certificate2(pfxFile, "password");
   x.UseHttps(certificate);
  };
 }
}

Then run dotnet xxx.dll --server in the command line window. The urls https://www.example.com:port can be used.

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

For more articles related to the use of HTTPS (SSL) in ASP.NET Core Kestrel, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn