0

这个问题与以下可能有帮助的帖子有关:Azure DevOps CI/CD and Separating Connection Strings from Source Control

我目前正在根据 Imar Spaanjaars 的一篇名为ASP.NET N-Layered Applications的文章开发一个 N-Layered 项目

我正在尝试实施Azure Key Vault,我猜你可以说,从应用程序本身抽象秘密。

目标

我想使用这个 N 层概念实现Azure Key Vault 。我有一个位于NLayer-Spaanjaars.ContactManager的示例项目

问题

我试图弄清楚如何使用Key Vault Syntax Reference来正确检索带有实体框架的秘密(连接字符串)。

更新 2019/2/22

如评论中所述,我试图找出如何在运行时使用非核心应用程序上的值injectoverride连接字符串。Key Vault.Net Web API

4

1 回答 1

0

我设法通过像这样修改我的 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;
    }
}
于 2019-03-07T16:05:16.467 回答