这个问题涉及以下可能有用的帖子:Azure DevOps CI/CD and Separating Connection Strings from Source Control
我目前正在根据Imar Spaanjaars的一篇名为ASP.NET N-Layered Applications的文章开展一个N层项目。
我试图实现Azure Key Vault,我想你可以说,从应用程序本身抽象秘密。
目标
我想使用这个N-Tier概念实现Azure Key Vault。我有一个样本项目位于NLayer-Spaanjaars.ContactManager
问题
我试图弄清楚如何使用Key Vault Syntax Reference正确检索Entity Framework的秘密(连接字符串)。
更新2019/2/22
正如评论中所述,我试图找出inject
或override
如何在运行时连接字符串与非核心Key Vault
应用程序上的.Net Web API
的值。
我设法通过修改我的DbContext来实现这一点:
public class MyContext : BaseDataContext {
public MyContext()
: this(GetDbConnection()) {
}
public MyContext(string connectionString)
: base(connectionString) {
}
public static string GetDbConnection() {
// Get the value from the AppSettings section in the Web.config file that will be updated by Key Vault
var connectionString = ConfigurationManager.AppSettings["{key-vault-secret-name}"];
// Return the connection string value above, if blank, use the connection string value expected in the Web.config
return string.IsNullOrWhiteSpace(connectionString) ? "MyContext" : connectionString;
}
}