如何让本地ASP.Net MVC应用通过HTTPS(而非HTTP)安全运行?
为ASP.NET MVC项目在IIS Express启用HTTPS的解决步骤
- 打开项目属性:右键项目 → 属性 → 切换到「Web」选项卡
- 配置服务器参数:
- 服务器选择「IIS Express」
- 勾选「启用SSL」,将「项目URL」修改为
https://localhost:6840,确认「SSL URL」同步为该地址
- 调整项目配置文件:
打开项目根目录下的Properties/launchSettings.json,确保iisSettings节点配置正确:"iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:6840", "sslPort": 6840 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } - 信任自签名证书:
访问https://localhost:6840,点击地址栏证书警告 → 查看证书 → 安装证书 → 选择「本地计算机」→ 下一步 → 选择「将所有证书放入下列存储」→ 浏览选中「受信任的根证书颁发机构」→ 完成 - 重启服务:关闭当前IIS Express实例,重启Visual Studio后重新启动项目
额外排查点
- 用命令
netstat -ano | findstr :6840检查端口是否被占用,若占用则更换端口或结束对应进程 - 若需要强制HTTP跳转HTTPS,可在
web.config中添加重写规则:<system.webServer> <rewrite> <rules> <rule name="HTTP to HTTPS redirect" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="off" ignoreCase="true" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer>
内容的提问来源于stack exchange,提问作者Daniel_Kamel
相关产品推荐
相关产品推荐

