asp.net core 2使用本地https证书
先在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证书的更多相关文章
- 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= ...
- 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日. 在目前介 ...
- .NET 6学习笔记(7)——ASP.NET Core通过配置文件启用HTTPS
本质上我还是一个Windows App Developer,所以虽然会做一些ASP.NET Core的工作,但通常这些ASP.NET Core的程序会托管在Windows Service上,并且大部分 ...
- ASP.NET Core Kestrel 中使用 HTTPS (SSL)
在ASP.NET Core中,如果在Kestrel中想使用HTTPS对站点进行加密传输,可以按照如下方式 申请证书 这一步就不详细说了,有免费的和收费的,申请完成之后会给你一个*.pfx结尾的文件. ...
- [译]ASP.NET Core 2.0 本地文件操作
问题 如何在ASP.NET Core 2.0中受限地访问本地目录和文件信息? 答案 新建一个空项目,修改Startup类,添加访问本地文件所需的服务: public void ConfigureSer ...
- 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 ...
- ASP.NET Core Kestrel部署HTTPS
ASP.NET Core配置 Kestrel部署HTTPS.现在大部分网站已经部署HTTPS,大家对于安全越来越重视. 今天简单介绍一下ASP.NET Core 部署HTTPS,直接通过配置Kestr ...
- 如何在docker配置asp.net core https协议
本文参考自<Step by step: Expose ASP.NET Core over HTTPS with Docker> 自从微软发布.net core以来,就在许多社区掀起了讨论, ...
- 【ASP.NET Core】自己编程来生成自签名的服务器证书
如果项目不大,或者是客户公司内部使用,或者不想花钱购买证书,又或者用于开发阶段测试--完全可以使用自签名证书. 所谓自签,就是自己给自己签名颁发的证书,自给自足,丰衣足食. 生成证书的方法和工具很多, ...
- [转帖]ASP.NET Core的Kestrel服务器
ASP.NET Core的Kestrel服务器 https://cloud.tencent.com/developer/article/1023247 在这篇文章中: 何时使用Kestrel和反向代理 ...
随机推荐
- Sonar 扫描之分析参数介绍
Sonar扫描之分析参数介绍 强制参数 服务器 Key 描述 默认 sonar.host.url 服务器网址 http://localhost:9000 项目配置 Key 描述 默认 sonar.pr ...
- hbuilderx+香蕉云编生成ios证书和上架教程
现在很多公司都使用uniapp作为底层框架来开发app应用,而uniapp的开发工具hbuilderx云打包的时候,需要证书和证书profile文件. 假如是ios应用,则还需要上架到appstore ...
- 2、Git之Windows版本的安装
2.1.下载Git 官网下载地址:https://git-scm.com/download/ 最初,Git 是用于管理 Linux 社区的代码,所以在很长的一段时间内,Git 只能安装在 Linux ...
- 【Java】再谈Springboot 策略模式
第一次使用策略模式是一年前的一个项目: https://www.cnblogs.com/mindzone/p/16046538.html 当时还不知道Spring支持集合类型的自动装配 在最近一个项目 ...
- 【DataBase】MySQL 26 存储过程
一.概述 存储过程&函数,类似编程语言的方法 什么是方法? 完成特定功能的一组语句 方法的特点 1.可重用性 2.简化操作 二.存储过程[ Stored Procedures]: 一组预先编译 ...
- 【JS】01 JavaScript概述
感觉上来谈这个前端,结构的部分是使用了标签语言定义了页面的文档内容 但是XML和HTML的功能完全相反,XML被用于存储信息,而HTML则用于定义网页结构,辅助其他内容渲染 然后C3又可以通过选择器这 ...
- 【Spring】08 后续的学习补充 vol2
1.[纯注解配置实现] 首先实现纯注解配置的第一件事情就是删除XML文件 然后编写一个类用于代替XML文件实现配置功能: 使用@Configuration将这个类注册为Spring容器 使用@Comp ...
- 关于Isaac Gym的两个版本比较:IsaacGymEnvs/omni.isaac.gym
原文地址: https://zhuanlan.zhihu.com/p/590468555 重点: IsaacGymEnvs (IGE)和 omni.isaac.gym (OIG)是两个东西. 原文内容 ...
- 为什么被要求避免使用orcid
前段时间收到了最高机构的通知,要求不要使用orcid,并对之前使用的情况进行自查.得到这个通知,我其实还是蛮无感的,毕竟我一篇论文也没法过,而且也没有用过这个orcid,虽然我自己也是有这个的. 关于 ...
- TensorFlow和pytorch中的pin_memory和non_blocking设置是做什么的,又是否有用???(续2)
接前文: TensorFlow和pytorch中的pin_memory和non_blocking设置是做什么的,又是否有用??? TensorFlow和pytorch中的pin_memory和non_ ...