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. ...
随机推荐
- git fetch origin
可以运行 git fetch origin 来同步远程服务器上的数据到本地.该命令首先找到 origin 是哪个服务器(本例为 git.ourcompany.com),从上面获取你尚未拥有的数据,更新 ...
- PHP7新特性之类型声明
今天我在这里总结下PHP7主要的新特性. 1.类型声明 做过php开发的小伙伴们都知道,php7以前的版本变量是不需要声明类型的,函数返回值也是不需要声明类型的,总之,在我们的脑海中就没有这么回事.可 ...
- MySQL_数据库命名规范及约定
操作规范 如无说明,建表时一律采用innodb引擎: 如无说明,数据库表编码集(utf8,utf8_bin)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf ...
- Win10下安装LabelImg以及使用--LabelImg
labelImg是图片标注软件,用于数据集的制作.标注等等.下面介绍labelImg的安装过程. 我用的是anaconda,所以以anaconda prompt作为终端: 在Anaconda Prom ...
- 网络基础 CAS协议学习总结
架构介绍 系统组件 CAS服务器和客户端构成了CAS系统体系结构的两个物理组件,它们通过各种协议进行通信. CAS服务器 CAS服务器是基于Spring Framework构建的Java servle ...
- 30FPS和120FPS在游戏中的区别
30FPS和120FPS的区别: 从动画上,时间尺度更小,渲染的时候物体单帧移动距离更小从物理引擎计算上,每一次的迭代更细致,计算更精确从渲染上:从触摸事件上,响应更及时,从触摸到屏幕,到系统捕捉,到 ...
- sql语句排序无效的问题
数据可视化时因为数据类型排序无效的问题:这是由于你要排序的类型是String类型的而ORDER BY 方法排序要求整数型. 这就需要在ORDER BY 后加 CAST(需要排序的字段 AS UNSIG ...
- 使用 useState 管理响应式状态
title: 使用 useState 管理响应式状态 date: 2024/8/1 updated: 2024/8/1 author: cmdragon excerpt: 摘要:本文详细介绍了在Nux ...
- 【Layui】01 快速入门
[原生JavaScript 与 JQuery] <!DOCTYPE html> <html lang="en"> <head> <meta ...
- IEEE TCDS 专刊"Embodied AI in Indoor Robotics"征稿通知
原文地址: https://mp.weixin.qq.com/s/Z-U4EO6FCF703yMwHXAq5A 随着深度学习和强化学习在机器人学领域的迅猛发展,尤其是大型语言模型的创新进步,具身人工智 ...