0

I have a project with an appsettings.Development.json file, to which I've added the following section:

"ConnectionStrings": {
    "Database one": "<Host...;Username=...;Password=...;etc.>"
}

When I search the internet for instructions on how to encrypt the ConnectionStrings section, I keep finding instructions that try to call functions that my IDE (VSCode) doesn't recognize (it doesn't even suggest that I must download a NuGet or import a using), which leads me to believe that it's all outdated, just as this person did. But what confuses me is that even the Microsoft documentation (see this link and this link), which is supposed to be up to date, is instructing me to use the functions that VSCode doesn't recognize, such as OpenExeConfiguration, SectionInformation, IsProtected, and ProtectSection.

Below is my best attempt at creating working code from the examples I've found, but it doesn't work yet because VSCode is still unable to recognize SectionInformation and ProtectSection:

public void ProtectSection(ConfigurationManager cManager)
{
    try
    {
        var section = cManager.GetSection("ConnectionStrings") as ConfigurationSection;

        if (!section.SectionInformation.IsProtected)
        {
            section.ProtectSection("ConnectionStrings");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
  • 1
    You are using a method that is documented for ASP.NET applications - do you have an ASP.Net application? Also that documentation is about 8 or 9 years old - I don't think it is .NET 7 – topsail Dec 27 '22 at 17:00
  • It's the only documentation I keep finding. I'm using ASP.NET Core 7.0. I created the project using the "ASP.NET Core with Angular" template that Visual Studio 2022 provides. – HasQuestionsAndAnswers Dec 27 '22 at 17:05
  • 1
    You may find this article worth reading for dealing with secrets in modern .NET (hint: skip encrypting appsettings.json): https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-7.0&tabs=windows – Luke Dec 27 '22 at 21:23
  • That was what I needed. I'll accept it as the answer if you convert it to an answer and explain why the approach I was going for is inadvisable (and outdated?). – HasQuestionsAndAnswers Dec 28 '22 at 11:17

0 Answers0