首页 > 解决方案 > 源代码中的 .net 核心连接字符串

问题描述

我正在研究实体框架的 .net 核心示例。

默认示例在数据库上下文的构造函数中包含以下内容。

#warning To protect potentially sensitive information in your connection string, you should move it out of source code. 
See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer("Server=servername");

正如警告所述,链接中没有提到它,我已将此反馈提供给 Microsoft。https://github.com/aspnet/EntityFramework.Docs/issues/1269

是否有存储 sql 连接字符串的首选做法?如果连接字符串在 appsettings.json 中,它仍然会在源代码管理中进行跟踪。包含来自 appsettings.json 的未跟踪文件的最佳解决方案是什么?

标签: c#.net-coreentity

解决方案


如果攻击者可以访问您的源代码信息,那么无论如何您都会被搞砸。

编译的 .NET 代码可以被反编译,从而允许攻击者泄露敏感数据,例如 db 用户密码。

配置文件可以在发布前加密以隐藏敏感数据。请参阅https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/connection-strings-and-configuration-files#encrypting-configuration-file-sections-using-protected-configuration


推荐阅读