博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows IIS ASP.NET Core中创建和使用HTTPS自签名证书
阅读量:4550 次
发布时间:2019-06-08

本文共 2156 字,大约阅读时间需要 7 分钟。

为什么要用Https就不说了。

第一步:创建自签名的证书。在Windows下开启PowerShell,将以下文字粘贴进去:

# setup certificate properties including the commonName (DNSName) property for Chrome 58+$certificate = New-SelfSignedCertificate `    -Subject 改成自己想要的标题不要带乱七八糟的符号(安装证书的时候会显示这个) `    -DnsName 友好域名 `    -KeyAlgorithm RSA `    -KeyLength 2048 `    -NotBefore (Get-Date) `    -NotAfter (Get-Date).AddYears(2) `    -CertStoreLocation "cert:CurrentUser\My" `    -FriendlyName "证书的友好名称,在IIS指定的时候显示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 "证书的密码" -Force -AsPlainText$pfxFilePath = "c:\tmp\证书的名称.pfx"$cerFilePath = "c:\tmp\证书的名称.cer"# create pfx certificateExport-PfxCertificate -Cert $certificatePath -FilePath $pfxFilePath -Password $pfxPasswordExport-Certificate -Cert $certificatePath -FilePath $cerFilePath# import the pfx certificateImport-PfxCertificate -FilePath $pfxFilePath Cert:\LocalMachine\My -Password $pfxPassword -Exportable# trust the certificate by importing the pfx certificate into your trusted rootImport-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 $pfxFilePathRemove-Item $cerFilePath

  把汉字部分修改成你想要的,然后运行一下,就可以在C:\tmp下面找到你的证书了,一般把它放在网站根目录下即可。

二、站点配置(ASP.NET Core 2.1)

* public void ConfigureServices(IServiceCollection services) 部分:

services.AddMvc(options =>

{
options.Filters.Add(new RequireHttpsAttribute());//所有请求都使用HTTPS
})

* public void Configure(IApplicationBuilder app, IHostingEnvironment env) 部分:

var options = new RewriteOptions().AddRedirectToHttps();

app.UseRewriter(options);
app.UseHttpsRedirection();

三、IIS配置:

经过这几步,你的网站就变成Https的了。

 

转载于:https://www.cnblogs.com/imes/p/9810277.html

你可能感兴趣的文章
sql中写标量函数生成大写拼音首字母
查看>>
ASP.NET Core 2.1 : 十五.图解路由(2.1 or earler)
查看>>
服务器返回状态码说明
查看>>
GitHub for Windows提交失败“failed to sync this branch”
查看>>
linux 安装 git
查看>>
Margin
查看>>
完成登录与注册页面的前端
查看>>
centos 源码安装php7
查看>>
Log4j详细教程
查看>>
UVa-1368-DNA序列
查看>>
ConfigParser模块
查看>>
如何开发优质的 Flutter App:Flutter App 软件测试指南
查看>>
决胜Flutter 第一章 熟悉战场
查看>>
如何开发优质的 Flutter App:Flutter App 软件调试指南
查看>>
决胜经典算法之冒泡排序
查看>>
决胜经典算法之选择排序
查看>>
11、求二进制中1的个数
查看>>
【nodejs】让nodejs像后端mvc框架(asp.net mvc)一样处理请求--请求处理结果适配篇(7/8)...
查看>>
CodeForces 731A Night at the Museum
查看>>
MySQL 删除数据库
查看>>