Azure Key保管库连接字符串和N层设计 [英] Azure Key Vault Connection Strings and N-Layered Design
问题描述
此问题与以下帖子有关,可能对您有所帮助: Azure DevOps CI/CD和将连接字符串与源代码控制分开
This question relates to the following post which maybe helpful: Azure DevOps CI/CD and Separating Connection Strings from Source Control
I'm currently working on an N-Layered project based off of an article by Imar Spaanjaars named ASP.NET N-Layered Applications
我正在尝试将 Azure Key Vault 实施为,我猜你可以说,是应用程序本身的抽象机密.
I'm trying to implement Azure Key Vault to, I guess you can say, abstract secrets from the application itself.
目标
我想使用此N-实现 Azure Key Vault 层级概念.我有一个示例项目,位于 NLayer-Spaanjaars.ContactManager
I want implement Azure Key Vault using this N-Tier concept. I have a sample project located at NLayer-Spaanjaars.ContactManager
问题
我正在尝试弄清楚如何使用 Key Vault语法参考,以使用实体框架正确检索机密(连接字符串).
I'm try to figure out how to use Key Vault Syntax Reference to properly retrieve the secret(s) (connection string) with Entity Framework.
更新2019/2/22
如评论中所述,我试图找出如何在运行时使用非Core .Net Web API
应用程序上的Key Vault
值来inject
或override
的连接字符串.
As stated in the comments, I'm trying to find out how to inject
or override
the connection string at runtime with values for the Key Vault
on a non-Core .Net Web API
app.
推荐答案
我设法通过修改我的DbContext来使它工作:
I managed to get this working by modifying my DbContext like so:
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;
}
}
这篇关于Azure Key保管库连接字符串和N层设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!