3

I want to encrypt the connection string in appsetings.json and decrypt before passing it to options.UseSqlServer(Configuration["Database:Connection"])) in ConfigureServices method. Unfortunately there is not much about this on internet about asp.net core. The connection string looks like the following:

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

  }

I might also like to so the same with other setting objects, following is so far I have gone by looking around but I guess I am lost. Any recommendations, highly appreciated. Thank you.

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();
    }
}
Raj
  • 343
  • 2
  • 6
  • 16
  • try searching for "c# encryption example" – Les Sep 26 '17 at 14:19
  • Possible duplicate of [Encrypt and decrypt a string](https://stackoverflow.com/questions/202011/encrypt-and-decrypt-a-string) – Les Sep 26 '17 at 14:27
  • Why you want to hide this data? Any algorithm you "invent" will not protect this data, if everything that is needed to "decrypt" in contained in app itself. – Dmitry Sep 26 '17 at 16:47
  • The application gets installed on local IIS, and app accesses SQL server from local network. I have connection string in the appsettings.json and I want to encrypt the connection part. Is there any way to do so? – Raj Sep 26 '17 at 18:28
  • The ask here was how to encrypt from appsettings.json file, not how to do simple string encryption. Doing this through framework is not straightforward. To say this is duplicate is disingenuous. – phitch Jul 23 '19 at 22:38

0 Answers0