0

I've followed this answer and now I'm not sure how to get the correct values back and use them in the code.

This is the configuration section i would like to use:

  "ConnectionStrings": {
    "connString1": "CfDJ8P1kYd0KR1J........",
    "connString2": "CfDJ8P1kYd0KR1J........"
  }

And this is my Startup.cs file:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);


            services.AddProtectedConfiguration();

            services.ConfigureProtected<ProtectedConnectionStrings>(Configuration.GetSection("ConnectionStrings"));

            services.AddDbContext<MyDBContext>(options =>
            options.UseSqlServer(Configuration.GetSection("ConnectionStrings")["connString2"]));
.
.
.


But the values i get from the configuration are not decrypted.
Furthermore, the object returned from Configuration.GetSection() is a regular ConfigurationSection object (and not the custom ProtectedConfigurationSection object i have).

Are the encrypted values supposed to be stored in the appsettings.json file? or they are stored in the key-.... file?

What am I doing wrong?

1 Answers1

0

You have to use DI to get unencrypted values from your service.

public class MySQLClass
{
    public MySQLClass(IOptions<ConnectionStrings> connectionStrings)
    {
        string unencryptedCS = connectionStrings.Value.connString1;
    }
}
SzymonB
  • 133
  • 6