Azure Key Vault连接字符串和N层设计

问题描述 投票:0回答:1

这个问题涉及以下可能有用的帖子: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

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

c# entity-framework azure azure-keyvault n-layer
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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.