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和反向代理 ...
随机推荐
- 基础-数组_C语言
C 语言支持数组数据结构,它可以存储一个固定大小的相同类型元素的顺序集合.数组是用来存储一系列数据,但它往往被认为是一系列相同类型的变量. 数组的声明并不是声明一个个单独的变量,比如 runoob0. ...
- DASCTF 2023六月挑战赛|二进制专项 PWN (上)
DASCTF 2023六月挑战赛|二进制专项 PWN (上) 1.easynote edit函数对长度没有检查 free函数存在UAF漏洞 思路:1.通过堆溢出,UAF,修改size位达到堆块重叠,使 ...
- 云原生 .NET Aspire 8.1 新增对 构建容器、编排 Python的支持
.NET Aspire 用于云原生应用开发,提供用于构建.测试和部署分布式应用的框架,这些应用通常利用微服务.容器.无服务器体系结构等云构造.2024年7月23日宣布的新 8.1 版本是该平台自 5 ...
- Python将本地文件上传到服务器
1.首先本地有一个文件"E:\Double\python\dataCheck\html_detail\20221206140345_activeBug.html",我需要上传到服务 ...
- 【MongoDB】Re02 文档CRUD
三.文档操作(行记录) 不管comment集合是否存在,直接在comment集合中创建一份文档 > db.comment.insert({"articleid":" ...
- 【Java】Generic 泛型
Generic 泛型 为什么需要泛型? 集合容器再设计阶段/声明阶段不能确定这个容器实际存储的是什么类型的对象 JDK5 以前只能把元素设计为Object基类 在JDK5之后用泛型来约束对象类型 除了 ...
- 【Spring】03 XML配置
Alias别名设置 可以为一个Bean的ID再设置一个ID 多一个可用标识,大概... 在获取实例注入参数时,两个标识都可以使用 除了Alias可以设置别名之外,Bean的标签本身也可以设置第二别名 ...
- 纪念IE浏览器退役,哈哈哈!!!
网址: https://haokan.baidu.com/v?pd=wisenatural&vid=16024148879625055169 ========================= ...
- 在oracle中将一行字符串拆分成多行
例如,有如下一张表,表名为bk_test.插入了以下数据: CREATE TABLE BK_TESK(id varchar2(10),s varchar2(20)); insert into BK_T ...
- typeof、offsetof、container_of
typeof 用于获取一个对象的类型,比如: unsigned int a = 1; // typeof (a) is unsigned int short b = 2; // typeof (b ...