先在PowerShell里运行以下, 生成证书:

# setup certificate properties including the commonName (DNSName) property for Chrome 58+
$certificate = New-SelfSignedCertificate `
-Subject localhost `
-DnsName localhost `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-NotBefore (Get-Date) `
-NotAfter (Get-Date).AddYears(2) `
-CertStoreLocation "cert:CurrentUser\My" `
-FriendlyName "Localhost Certificate for .NET Core" `
-HashAlgorithm SHA256 `
-KeyUsage DigitalSignature, KeyEncipherment, DataEncipherment `
-TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.1")
$certificatePath = 'Cert:\CurrentUser\My\' + ($certificate.ThumbPrint) # create temporary certificate path
$tmpPath = "C:\tmp"
If(!(test-path $tmpPath))
{
New-Item -ItemType Directory -Force -Path $tmpPath
} # set certificate password here
$pfxPassword = ConvertTo-SecureString -String "YourSecurePassword" -Force -AsPlainText
$pfxFilePath = "c:\tmp\localhost.pfx"
$cerFilePath = "c:\tmp\localhost.cer" # create pfx certificate
Export-PfxCertificate -Cert $certificatePath -FilePath $pfxFilePath -Password $pfxPassword
Export-Certificate -Cert $certificatePath -FilePath $cerFilePath # import the pfx certificate
Import-PfxCertificate -FilePath $pfxFilePath Cert:\LocalMachine\My -Password $pfxPassword -Exportable # trust the certificate by importing the pfx certificate into your trusted root
Import-Certificate -FilePath $cerFilePath -CertStoreLocation Cert:\CurrentUser\Root # optionally delete the physical certificates (don’t delete the pfx file as you need to copy this to your app directory)
# Remove-Item $pfxFilePath
Remove-Item $cerFilePath

2, copy到项目根目录

3, 根目录下放如下文件:

certificate.json:

{
"certificateSettings": {
"fileName": "localhost.pfx",
"password": "YourSecurePassword"
}
}

3, 修改以下源码文件:

Programe.cs:

public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddEnvironmentVariables()
.AddJsonFile("certificate.json", optional: true, reloadOnChange: true)
.AddJsonFile($"certificate.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
.Build(); var certificateSettings = config.GetSection("certificateSettings");
string certificateFileName = certificateSettings.GetValue<string>("filename");
string certificatePassword = certificateSettings.GetValue<string>("password"); var certificate = new X509Certificate2(certificateFileName, certificatePassword); var host = new WebHostBuilder()
.UseKestrel(
options =>
{
options.AddServerHeader = false;
options.Listen(IPAddress.Loopback, 44321, listenOptions =>
{
listenOptions.UseHttps(certificate);
});
}
)
.UseConfiguration(config)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls("https://localhost:44321")
.Build(); host.Run();
}
}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
// ... services.AddMvc(
options =>
{
options.SslPort = 44321;
options.Filters.Add(new RequireHttpsAttribute());
}
); services.AddAntiforgery(
options =>
{
options.Cookie.Name = "_af";
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.HeaderName = "X-XSRF-TOKEN";
}
); // ...
}


来源参考:https://my.oschina.net/u/4278498/blog/3494495

asp.net core 2使用本地https证书的更多相关文章

  1. ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1)

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  2. ASP.NET Core 1.0 部署 HTTPS

    ASP.NET Core 1.0 部署 HTTPS ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1) 提示 更新时间:2016年01月23日. 在目前介 ...

  3. .NET 6学习笔记(7)——ASP.NET Core通过配置文件启用HTTPS

    本质上我还是一个Windows App Developer,所以虽然会做一些ASP.NET Core的工作,但通常这些ASP.NET Core的程序会托管在Windows Service上,并且大部分 ...

  4. ASP.NET Core Kestrel 中使用 HTTPS (SSL)

    在ASP.NET Core中,如果在Kestrel中想使用HTTPS对站点进行加密传输,可以按照如下方式 申请证书 这一步就不详细说了,有免费的和收费的,申请完成之后会给你一个*.pfx结尾的文件. ...

  5. [译]ASP.NET Core 2.0 本地文件操作

    问题 如何在ASP.NET Core 2.0中受限地访问本地目录和文件信息? 答案 新建一个空项目,修改Startup类,添加访问本地文件所需的服务: public void ConfigureSer ...

  6. ASP.NET Core中代码使用X509证书,部署到IIS上后报错:System cannot find the specified file 的解决办法(转载)

    问: I am trying to embrace the mysteries of SSL communication and have found a great tutorial on this ...

  7. ASP.NET Core Kestrel部署HTTPS

    ASP.NET Core配置 Kestrel部署HTTPS.现在大部分网站已经部署HTTPS,大家对于安全越来越重视. 今天简单介绍一下ASP.NET Core 部署HTTPS,直接通过配置Kestr ...

  8. 如何在docker配置asp.net core https协议

    本文参考自<Step by step: Expose ASP.NET Core over HTTPS with Docker> 自从微软发布.net core以来,就在许多社区掀起了讨论, ...

  9. 【ASP.NET Core】自己编程来生成自签名的服务器证书

    如果项目不大,或者是客户公司内部使用,或者不想花钱购买证书,又或者用于开发阶段测试--完全可以使用自签名证书. 所谓自签,就是自己给自己签名颁发的证书,自给自足,丰衣足食. 生成证书的方法和工具很多, ...

  10. [转帖]ASP.NET Core的Kestrel服务器

    ASP.NET Core的Kestrel服务器 https://cloud.tencent.com/developer/article/1023247 在这篇文章中: 何时使用Kestrel和反向代理 ...

随机推荐

  1. THINKPHP的CURD是什么意思?

    thinkphp的CURD是什么意思? 解释如下: 增:C    Create   在thinkphp的代码里面对应的方法是add(); 改:U   Update  在thinkphp的代码里面对应的 ...

  2. html5 video标签 去掉边框黑线

    只需在样式中添加: mix-blend-mode: darken; 注意:IE和Edge浏览器不支持mix-blend-mode属性

  3. 写写java中的optional

    当我们写代码的时候经常会碰见nullpointer,所以在很多情况下我们需要做各种非空的判断.JDK8中引入了optional,他是一个包装好的类,我们可以把对象传入optional对象中,接下来就可 ...

  4. python_xecel

    移动并重命名工作簿 1 from pathlib import Path # 导入pathlib模块的path类 2 import time 3 4 # Press the green button ...

  5. 基于 SASL/SCRAM 让 Kafka 实现动态授权认证

    一.说明 在大数据处理和分析中 Apache Kafka 已经成为了一个核心组件.然而在生产环境中部署 Kafka 时,安全性是一个必须要考虑的重要因素.SASL(简单认证与安全层)和 SCRAM(基 ...

  6. HDP 源码集

    HDP 各个组件的源码(含历史各个版本) 分支 组件 标签 最后发版时间 地址 hadoop 2256 2020-12-21 17:44 https://gitee.com/piaolingzxh/h ...

  7. 【MySQL】拆分经纬度字段

    数据结构: 表中一字段存储经度和纬度: +---------------------------+ | INSTALL_LOLA_NUM | +---------------------------+ ...

  8. 【Redis】04 配置文件分析

    配置文件Redis.conf注释信息: 1.启动项: 启动Redis要求必须加上配置文件redis.conf路径作为第一参数加载 文档样例: ./redis-server /path/to/redis ...

  9. 人机协同的半自动人形机器人 —— Covariant公司的RFM-1机器人

    Covariant公司的RFM-1机器人实现了一个极为有意思的功能,那就是在机器人执行任务的过程中如果遇到无法处理的情况下就会停止下来然后等待人类的语言指示,比如:夹具向上移动2cm,更换更大型号的夹 ...

  10. rllab —— 强化学习 算法模块库 (环境配置失败!!!) (已经不进行维护更新,已被淘汰,由于一些老旧代码还有可能关联因此fork了下,复制了份原始项目)

    代码地址: https://gitee.com/devilmaycry812839668/rllab 由于官方代码地址在github上,外网偶尔会出现网络连接中断的问题因此在国内代码库中fork了一个 ...