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. ...
随机推荐
- 解决方案 | 在 Tkinter 中导入 pywinauto/pyautogui 时窗口大小发生变化
上面问题也可以换一个说法,pywinauto/pyautogui 时改变了tkinter的原有的窗口大小.这个问题困扰了我好几天而且网上有这样的问题但是并没有答案,今天摸索出答案给大家分享下.解决方法 ...
- Loggie Geek Camp 火热启动,社区大神带你玩开源
由开源构建的协作模式,正成为描绘数字蓝图.创造美好未来的主航道,中国工商银行.阿里云等组织已经在网易数帆"架构开发.内核开源"理念中与后者达成合作,来自华为.阿里云.中国移动.eB ...
- php 模型浏览器
docker安装 文档:https://github.com/SeleniumHQ/docker-selenium docker run -d -p 4444:4444 -p 7900:7900 -- ...
- Bond4配置
Bongding聚合链路工作模式 > bond聚合链路模式共7种:0-6Mode > bond 0 负载均衡 轮询方式往每条链路发送报文,增加带宽和容错能力.容易出现数据包无序到达的问题, ...
- Jmeter强制结束线程
例子:正常的线程是执行2次请求 1.需要实现结果 执行请求1后,判断test1=100,强制结束线程 执行请求1后,判断test1 != 100,继续执行请求2 2. 线程组改造 在请求1后面增加[i ...
- DrawIO安装及基本使用教程
1.DrawIO简介 DrawIO 是一款开源免费且功能强大的绘图工具,可以用于绘制流程图.组织结构图.网络图.UML图等各种类型的图表: DrawIO 支持多种文件格式,包括XML.PNG.SVG等 ...
- 【Spring-Security】Re08 Thymeleaf权限控制 与 退出功能
一.需要的组件支持: 新版本这里的组件有些问题: https://blog.csdn.net/qq_36488647/article/details/104532754 https://blog.cs ...
- 【转载】 源码分析multiprocessing的Value Array共享内存原理
原文地址: http://xiaorui.cc/archives/3290 ========================================================= 当第一次 ...
- aarch64架构CPU下Ubuntu系统环境源码编译pytorch-gpu-2.0.1版本
准备事项: 1. pytorch源码下载: 源码的官方地址: https://github.com/pytorch/pytorch 但是这里我们不能简单的使用git clone命令下载,因为pytor ...
- Longley数据集——强共线性的宏观经济数据,包含GNP deflator(GNP平减指数)、GNP(国民生产总值)、Unemployed(失业率)、ArmedForces(武装力量)、Population(人口)、year(年份),Emlpoyed(就业率)。LongLey数据集因存在严重的多重共线性问题,在早期经常用来检验各种算法或计算机的计算精度
Longley数据集来自J.W.Longley(1967)发表在JASA上的一篇论文,是强共线性的宏观经济数据,包含GNP deflator(GNP平减指数).GNP(国民生产总值).Unemploy ...