Azure Key保管库连接字符串和N层设计 [英] Azure Key Vault Connection Strings and N-Layered Design

查看:59
本文介绍了Azure Key保管库连接字符串和N层设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与以下帖子有关,可能对您有所帮助: 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

根据Imar Spaanjaars的文章

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值来injectoverride的连接字符串.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆