源代码管理中的 .NET Core 环境变量

.NET Core environment variables in source control

我正在学习使用 .NET Core 5.0 框架,并且正在阅读 this article 关于管理用户机密的内容。来自 Node.js,我习惯于 .env 存储数据库连接凭证的文件。

查看样板文件时,例如 ASP.NET Boilerplate or clean-architecture-manga,我注意到数据库连接字符串仍在 appsettings.json 中,它不在 .gitignore 文件中。

"PersistenceModule": {
    "DefaultConnection": "Server=localhost;User Id=sa;Password=<YourStrong!Passw0rd>;Database=Accounts;"
},

我是不是对 ASP.NET 中的用户机密有什么误解? 每个人的数据库密码都一样吗?

您可能想看看这个 documentation。在 .Net Core 中,只要 appsettings.json 文件被正确标记,就可以在启动时通过命令行从环境变量将秘密插入到 appsettings.json 文件中。还可以使用存储在 Key Vault 中的值覆盖 appsettings.json 文件,例如 Hashicorp Vault 或 Microsoft 的 Azure Key Vault。