一、功能实现大步骤
1、首先得有一个域名(DNS),没有的话需要购买
2、申请一个SSL证书,可以去https://freessl.cn/,或者去阿里云里面的ssl控制台里面申请,有免费1年的
3、申请好了,就在你所在的域名服务商里面的DNS配置解析,ssl证书里面的 TXT 值
4、SSL中验证
二、具体实现
1、由于我没有DNS,只能在Windows本机设置一个本机DNS了
windows本机域名设置
进入 C:\Windows\System32\drivers\etc 下,找到hosts 并打开
找到NDS配置的地方,做如下修改:
2、SSL证书生成
我这里使用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 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
将以上内容中的中文文字替换掉,其中 DNS是你前面创建的域名,其中的 $tmpPath、$pfxFilePath、$cerFilePath的磁盘文件路径可以修改为自己想设置的路径
使用管理员身份运行powershell,将修改过的代码拷贝到powershell中,生成证书文件
然后,在配置的证书路径下会生成证书文件
3、创建.net core项目
创建一个.net core 项目(我这里是webapi)
Appsettings 中配置项目的ip地址,端口号,以及相关证书信息
"HostUrl": {
"RequestUrl": "192.168.1.45",//ip地址
"apiPort": "1905",//端口号
"enableHttps": "false", //是否启用https false 或true
"httpscertpath": "C:\\BoschRtnsCert\\BoschRtns.pfx", //证书路径
"httpscertpwd": "123456" //证书密码
}
然后在项目的Program中设置https及证书的加载
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var configuration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory).AddJsonFile("appsettings.json").Build();
string hostUrl = configuration["HostUrl:RequestUrl"];
string enablehttps = configuration["HostUrl:enableHttps"];
int port=Convert.ToInt32(configuration["HostUrl:apiPort"].ToString());
string certPath= configuration["HostUrl:httpscertpath"];
string certPwd= configuration["HostUrl:httpscertpwd"];
return WebHost.CreateDefaultBuilder(args)
//.UseUrls(hostUrl)
.UseKestrel(options =>
{
options.Listen(IPAddress.Parse(hostUrl), port, listenOptions =>
{
if(enablehttps == "true")
{
//加载证书和密码
listenOptions.UseHttps(certPath, certPwd);
}
});
})
.UseStartup<Startup>();
}
4、防火墙设置
在防火墙中,添加端口的进站规则
5、测试
1)使用https+ip地址访问
2)使用https+dns访问