温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

aspnetcore如何使用Let’s Encrypt支持https

发布时间:2021-09-08 13:54:44 来源:亿速云 阅读:132 作者:小新 栏目:编程语言

这篇文章主要为大家展示了“aspnetcore如何使用Let’s Encrypt支持https”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“aspnetcore如何使用Let’s Encrypt支持https”这篇文章吧。

Let’s Encrypt 是一个自动签发 https 证书的免费项目
Certbot 是 Let’s Encrypt 官方推荐的证书生成客户端工具

基本操作

0 准备工作

将要签发证书的域名(如 you.domain.com) 解析到一台可在外网正常访问的服务器

1 登录服务器

my-pc$ ssh user@you.domain.com

2 安装 certbot

这里以 Ubuntu 16.04 为例
各个系统的安装方式不一样,可以在官网首页查阅

my-server$ cat /etc/issue
Ubuntu 16.04.2 LTS \n \l

my-server$ sudo apt-get update
my-server$ sudo apt-get install software-properties-common
my-server$ sudo add-apt-repository ppa:certbot/certbot
my-server$ sudo apt-get update
my-server$ sudo apt-get install certbot 
my-server$ certbot --version
certbot 0.19.0

3 检查端口占用

如果服务器上的 443 端口正在被占用,请先关闭对应的服务进程
否则可能个导致 certbot 运行出错

4 以命令交互方式开始制作证书

my-server$ certbot certonly    --------> 开启命令

Saving debug log to /var/log/letsencrypt/letsencrypt.log

How would you like to authenticate with the ACME CA?
1: Spin up a temporary webserver (standalone)
2: Place files in webroot directory (webroot)

Select the appropriate number [1-2] then [enter] 
(press 'c' to cancel): 1  --------> 这里我们选择 1 standalone 模式
Plugins selected: Authenticator standalone, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): you@gmail.com  --------> 输入你的邮箱创建账号(只有第一次使用时会出现)

Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
(A)gree/(C)ancel: a  --------> 选择 A 同意 (只有第一次使用时会出现)

Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
(Y)es/(N)o: y  --------> 选择 Y 或 N (只有第一次使用时会出现)
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): you.domain.com   --------> 这里输入要制作证书的域名

Obtaining a new certificate
Performing the following challenges:
tls-sni-01 challenge for you.domain.com
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/you.domain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/you.domain.com/privkey.pem
   Your cert will expire on 2018-01-26. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le


my-server$ ls /etc/letsencrypt/live/you.domain.com/  --------> 证书文件目录
cert.pem  chain.pem  fullchain.pem  privkey.pem  README

看到 IMPORTANT NOTES 下方出现 Congratulations 就表明证书已经成功签发
可以进入 /etc/letsencrypt/live/you.domain.com/ 目录查看相关生成的文件

进阶操作

使用 80 端口

默认 standalone 是使用 443 端口,也就是说要停止服务器现在占用 443 端口的进程
我们也可以将其改为使用 80 端口,同样道理,这时需要停止 80 端口的占用

my-server$ certbot certonly --standalone -n --agree-tos --email you@gmail.com --preferred-challenges http -d you.domain.com

-n 非交互式
--email 指定账户
--agree-tos 同意服务协议

使用 nginx 模式

安装 nginx 和插件

my-server$ apt-get install nginx
my-server$ apt-get install python-certbot-nginx
my-server$ certbot --nginx -d you.domain.com

使用 manual 模式

manual 模式其实和 webroot 类似,主要的区别就是 certbot 自动向 webroot 添加文件的过程,变为你自己手动完成

my-server$ certbot certonly --manual -d you.domain.com

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for you.domain.com

NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
(Y)es/(N)o: y    --------> 选 Y

Create a file containing just this data:

_Pamo3hOMx37O-KHIioWO1P0hbTLZusEZ2a2iCf7PDQ.8CyQk-SfDMR10nk7DuGBFA4IEpVhm1OySU-nSItD1BE

And make it available on your web server at this URL:

http://you.domain.com/.well-known/acme-challenge/_Pamo3hOMx37O-KHIioWO1P0hbTLZusEZ2a2iCf7PDQ

Press Enter to Continue  
--------> 你需要保证 http://you.domain.com/.well-known/acme-challenge/_Pamo3hOMx37O-KHIioWO1P0hbTLZusEZ2a2iCf7PDQ 
这个 url 返回的结果为 _Pamo3hOMx37O-KHIioWO1P0hbTLZusEZ2a2iCf7PDQ.8CyQk-SfDMR10nk7DuGBFA4IEpVhm1OySU-nSItD1BE (这个数据是随机的,每次都不一样) 
当你做到后,按下回车键继续

Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/you.domain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/you.domain.com/privkey.pem
   Your cert will expire on 2018-01-27. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

查看和管理当前服务器上的所有证书

my-server$ certbot certificates

使用 https 证书

以 nginx 为例

server {
    listen 80;
    listen 443 ssl;
    server_name you.domain.com;
    ssl_certificate /etc/letsencrypt/live/you.domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/you.domain.com/privkey.pem; 
    # ...
}

letsencrypt 有什么限制

标准限制

  • 同一个顶级域名下的二级域名,一周做多申请 20 个

  • 一个域名一周最多申请 5 次

  • 1 小时最多允许失败 5 次

  • 请求频率需要小于 20 次/s

  • 一个 ip 3 小时内最多创建 10 个账户

  • 一个账户最多同时存在 300 个 pending 的审核

测试模式限制

在 certbot 命令后加上 --staging 参数,限制更松一些,但不是正式证书

  • 同一个顶级域名下的二级域名,一周做多申请 30000 个

  • 一个域名一周最多申请 30000 次

  • 1 小时最多允许失败 60 次

  • 一个 ip 3小时内最多创建 50 个账户

转换为aspnetcore可用的证书

openssl pkcs12 -export -out cert.pfx -passout pass:你的密码 -inkey privkey.pem -in cert.pem -certfile fullchain.pem

在aspnetcore使用,修改 Program.cs 文件加载证书

                .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>()
                    .UseKestrel(options => { 
                        options.AddServerHeader = false;
#if !DEBUG
                        options.ConfigureHttpsDefaults(co =>
                        {
                            var cert = new X509Certificate2(File.ReadAllBytes(@"./cert.pfx"), "你的密码");
                            co.ServerCertificate = cert;
                        });
#endif
                    });
                })

以上是“aspnetcore如何使用Let’s Encrypt支持https”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI