Vs Code, Visual Studio 2022, Angular and Live Server Running Through Https and IP Address
前言
之前就写过 angular cli, vs code liveserver, vs 2019 iis express 10, vs code kestrel 使用 https + ip. 但写的很乱. 这篇作为整理版.
默认情况下, 本地开发的 URL 是 localhost, ASP.NET Core 会有 build-in 的 Https, 游览器也会 trust 好好.
但是 localhost 不方便用手机链接. 所以改用 IP 会比较理想. 但是 IP 默认是没有 Https 的.
虽然没有 Https 也是可以跑, 但是会很不方便, 比如游览器的 address bar 会一直出现 (在手机), 因为它要提示你网站没有运行在 Https 上.
所以呢, 最好的办法就是支持 IP 同时支持 Https.
原理
和 production 的 SSL 类似, 但不是用 domain 而是 IP address, 还有 SSL 不是 CA 认证机构颁发的, 是 Self-signed 的.
2 个关键点.
第 1 是 Server 需要 Certificate, 不管是 Webpack, Angular, Live Server, ASP.NET Core (IIS / Kestrel), 我们都需要把 self-signed certificate 搞进去.
第 2 是游览器要 trust self-signed certificate.
Create Self-signed Certificate
首先需要有指定 IP 的 certificate. 按照这篇做就可以了. .key, .crt, .pfx 3 个 file 都要哦.
Trust Self-signed Certificate
PC端
游览器必须相信这个自签名证书. 通过 OS 的 MMC 去设置.
打开 start -> mmc
File -> Add/Remove Snap-in...
Certificates > Add > My user account
去 Trusted Root import 证书 .crt 就可以了.
记得要重启 Chrome 才有效果哦.
Mobile端
IOS trust: Installing an SSL Certificate on an iOS Device (Manually)
Android trust: setting > Passwords & security > Privacy > More security settings > Encryption and credentials > Install a certificate > CA certificate 和 Certificate > 去 google drive 选 .crt > 输入手机密码 > 安装成功后可以去 Trusted credentials 和 User credentials 看到。
Malaysia Unifi 5G Problem
有时候手机会突然连不上 IP Address (不管是 Live Server, Webpack Server, ASP.NET Core IIS Express 都会连不上), 原因不明. 这个时候可以试试 off on Wi-Fi, 或者 换成不是 5G, 然后重新链接.
Live Server
打开 settings.json 配置就可以了.
设置 IP and 链接 certificate (其它 Server 也是这个套路)
"liveServer.settings.host": "192.168.1.152",
"liveServer.settings.port": 4200,
"liveServer.settings.https": {
"enable": true,
"cert": "C:\\self-signed-certificate\\192.168.1.152.crt",
"key": "C:\\self-signed-certificate\\192.168.1.152.key",
"passphrase": ""
},
Angular
在 angular.json 添加设置
或者在启动 command 加上 parameters
ng serve --open --host=192.168.1.152 --port=4200 --ssl --ssl-key=C:\\self-signed-certificate\\192.168.1.152.key --ssl-cert=C:\\self-signed-certificate\\192.168.1.152.crt
IIS localhost Certificate
安装 IIS/Kestrel (通常是安装 Visual Studio 时一起装的) 的时候, 它自带了 localhost 的 certificate. 同时也 trust 了.
所以不用烦. 但如果某些原因不小心搞丢了. 可以通过 control panel -> programs -> iis express -> repair 来修复 (注意是 control panel 而不是 add/remove 哦)
然后通过 dotnet command trust
dotnet dev-certs https --trust dotnet dev-certs https --clean // 这样是清空
注: 有时候要重启才生效哦
VS Code & Visual Studio 2022 IIS / Kestrel
设置 IP
Kestrel 链接 certificate
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel((context, serverOptions) =>
{
serverOptions.ConfigureHttpsDefaults(options =>
{
options.ServerCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(
"C:\\self-signed-certificate\\192.168.1.152.pfx",
"password"
);
});
});
IIS 链接 certificate
这个步骤多了一些,
先把 certificate (.pfx) import to MMC Certificate Computer account (local machine)
这个和上面的 trust 不同, 这个是为了让 IIS 能用到证书. Live Server 和 Angular 是直接通过 path 链接到 certificate file. 但是 IIS 很麻烦, 它是通过 Windows 的 Certificate Store 才能拿到 certificate 的.
然后链接 certificate 需要 run iis command
打开 cmd, run as admin
cd 到 iis express 的 folder
cd C:\Program Files (x86)\IIS Express
执行 setupssiurl
IisExpressAdminCmd.exe setupsslUrl -url:https://192.168.1.152:44300/ -CertHash:00c3c0bbd89d011e0750e3497bfa4bac39d1ccb6
CertHash 是证书指纹, 到 MMC 证书查看.
这样 IIS 就知道, 当 192.168.1.152:44300 时, 使用这个 self-signed certificate.
Visual Studio 2022
以前 Visual Studio 2019 很麻烦 setup 的, 不过现在我测试 Visual Studio 2022 完全不需要 setup, 用回上面的 VS Code file 直接开 run 就可以了. 也不需要 run as admin.
不知道是不是进步了.
Vs Code, Visual Studio 2022, Angular and Live Server Running Through Https and IP Address的更多相关文章
- Code Runner,率先支持刚发布的 Visual Studio 2022!
Visual Studio 被不少网友成为"宇宙第一IDE".但是,我写✍ PHP.Java 和 C#,也都是用的 VS Code. 我所在的组,是 Visual Studio C ...
- [翻译]正式宣布 Visual Studio 2022
原文: [Visual Studio 2022] 首先,我们要感谢正在阅读这篇文章的你,我们所有的产品开发都始于你也止于你,无论你是在开发者社区上发帖,还是填写了调查问卷,还是向我们发送了反馈意见,或 ...
- 它来了!!!有史以来第一个64位Visual Studio(2022)预览版将在今夏发布!
美国时间2021年4月19日,微软产品研发部一位负责人Amanda Silver在其博客上发布一则<Visual Studio 2022>的消息,表示将在今年(2021年)夏天发布Visu ...
- .NET6系列:微软正式宣布Visual Studio 2022
系列目录 [已更新最新开发文章,点击查看详细] 首先,我们要感谢正在阅读这篇文章的你,我们所有的产品开发都始于你也止于你,无论你是在开发者社区上发帖,还是填写了调查问卷,还是向我们发送了反馈意 ...
- 乘风破浪,遇见Visual Studio 2022预览版(Preview),宇宙最强开发者工具首次迎来64位版本
简介 众所周知,我们从官方新闻来看,对Visual Studio 2022最大的期待莫过于:其是首个64位的Visual Studio,这个宇宙最强开发者工具一脚迈入了新的阶段. https://vi ...
- Visual Studio 2022 预览版3 最新功能解说
我们很高兴地宣布Visual Studio 2022 的第三个预览版问世啦!预览版3 提供了更多关于个人和团队生产力.现代开发和持续创新等主题的新功能.在本文中,我们将重点介绍Visual Studi ...
- Visual Studio 2022 Community 不完全攻略
0. 前言 建议结合视频阅读哦 Visual Studio 2022 Community 不完全攻略 有问题或者意见欢迎评论 ! 1. 下载&安装 Visual Studio Communit ...
- .NET6系列:Visual Studio 2022 线路图
系列目录 [已更新最新开发文章,点击查看详细] 在上一篇博客<Visual Studio 2022>中介绍了VS2022的性能改进与重要功能.本文主要介绍在 Visual Stud ...
- 微软发布了Visual Studio 2022 Preview 1 以及.NET 6 Preview 5
Microsoft 今天宣布了Visual Studio 2022 的第一个预览版,并且同时也发布了.NET 6 Preview 5. https://devblogs.microsoft.com/v ...
- Visual Studio 2022 Preview 1 和.NET 6 Preview 5 正式发布
具有里程碑意义的Visual Studio 2022 Preview 1正式发布,重点是64位,而没有增加新功能,并且同时也发布了.NET 6 Preview 5. https://devblogs. ...
随机推荐
- EasyBPM进销存之物料管理
本文是EasyBPM平台实现进销存系列中的一篇,主要讲述物料的相关的管理. 在ERP系统中,"物料"一词有着广泛的含义,它是所有产品.半成品.在制品.原材料.配套件.协作件.易耗品 ...
- [oeasy]python0143_主控程序_main
主控程序 回忆上次内容 上次把 apple.py 拆分成了 输入 主函数 引用模块中变量的时候 要带上包(module)名 get_fruits.a get_fruits.b 最终 拆分代 ...
- [oeasy]python0125_汉字打印机_点阵式打字机_汉字字形码
汉字字形码 回忆上次内容 IBM 将 ASCII 扩展之后 规定了 一个字节的字符集 并制作了 相应的字形库 添加图片注释,不超过 140 字(可选) 这种显示模式和字符大小之下 中文 ...
- 初读Nginx
Nginx反向代理:将前端发送的动态请求由Nginx转发到后端服务器 NGINX的好处: 可以缓存,提高访问速度 负载均衡:当请求量过大时,可以按指定方式均衡的分配给集群中的每台服务器 保证后端服务安 ...
- Scratch作品-巴黎2024奥运会
<Scratch作品-巴黎2024奥运会>是一款以巴黎2024年奥运会为主题的互动作品,专为儿童和青少年设计.通过Scratch编程语言,这个作品生动地再现了奥运会的精彩瞬间,结合了动 ...
- sql语句排序无效的问题
数据可视化时因为数据类型排序无效的问题:这是由于你要排序的类型是String类型的而ORDER BY 方法排序要求整数型. 这就需要在ORDER BY 后加 CAST(需要排序的字段 AS UNSIG ...
- js实现动态表格的添加
<!DOCTYPE html> <html lang="en"> <head> <title>Table_Simple CSS fo ...
- Jmeter函数助手6-time
time函数用于获取不同格式的当前时间(年月日时分秒). Format string for SimpleDateFormat (optional):时间格式,填入如yyyyMMdd-HHmmss.d ...
- go语言学习 “go.mod file not found”
遇到问题:安装go 的win安装包后 执行第一个代码: package main import "fmt" func main() { fmt.Println("Hell ...
- QWen2-72B-Instruct模型安装部署过程
最近在给我们的客户私有化部署我们的TorchV系统,客户给的资源足够充裕,借此机会记录下部署千问72B模型的过程,分享给大家! 一.基础信息 操作系统:Ubuntu 22.04.3 LTS GPU: ...