如何在appsettings.json asp.net核心中加密/解密字段/对象

时间:2017-09-26 14:12:43

标签: c# asp.net asp.net-core

我想在appsetings.json中加密连接字符串并在将它传递给ConfigureServices方法中的options.UseSqlServer(Configuration [" Database:Connection"])之前解密。不幸的是,在互联网上关于asp.net核心的内容并不多。连接字符串如下所示:

    "Database": {
    "Connection": "string"

  }

我可能也喜欢和其他设置对象一样,以下是到目前为止我已经四处寻找,但我想我迷路了。任何建议,高度赞赏。谢谢。

public class CustomConfigProvider : ConfigurationProvider, IConfigurationSource
{
    public CustomConfigProvider()
    {

    }

    public override void Load()
    {
        Data = UnencryptMyConfiguration();
    }

    private IDictionary<string, string> UnencryptMyConfiguration()
    {
        // do whatever you need to do here, for example load the file and unencrypt key by key
        //Like:
        var configValues = new Dictionary<string, string>
        {
            {"Database", "string"}

        };
        return configValues;
    }

    private IDictionary<string, string> CreateAndSaveDefaultValues(IDictionary<string, string> defaultDictionary)
    {
        var configValues = new Dictionary<string, string>
        {
            {"Database", "string"},

        };
        return configValues;
    }
    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return new CustomConfigProvider();
    }
}

0 个答案:

没有答案