2

When you create an ASP.NET Core website you have the option of using .NET Core or .NET Framework, depending on (among other things) whether or not your site will use .NET Framework components.

I need to reference a regular .NET Framework class library in my site, so I chose the .NET Framework option.

However, this class library makes use of appSettings defined in the app.config file. Now, if I place an app.config file in my ASP.NET Core website, and create an appSettings section and populate it with the fields I need, everything works.

However, these are sensitive keys (connection strings, etc.) so I want to be able to put them in an external file as described here: http://www.hanselman.com/blog/BestPracticesForPrivateConfigDataAndConnectionStringsInConfigurationInASPNETAndAzure.aspx

However, when I do this, I get an exception in the external library that the configuration setting is empty. I referenced the external file, which is in the same directory as the app.config in the root of the project (not the wwwroot folder) but the value always comes up empty. I tried putting the external config file in the wwwroot folder just out of desperation but that didn't work either.

The external reference works fine in the .NET Framework project as well as a console app I used to test it;only the .NET Core website seems to be unable to be able to handle it as an external reference. It only works if it's inside the app.config file directly.

Is pointing the configSource or file option for appSettings in app.config not supported in ASP.NET Core, or am I missing a step?

If it can't be done, how can I pass the necessary configuration over to the external class library which was not built with .net core?

EDIT: In summary: the class library is using System.Configuration, and NOT targeting .net core or standard.

I'm using a website in ASP.NET core on .NET Framework, and my problem is that app.config works, but ONLY if the appSettings are in the app.config file.

I want to move them to an external file (so the appSetting values are not checked into source) but this appears to not work.

My problem isn't that configuration doesn't work, it's that I can't put their values in a separate file.

1
  • This is pretty common. You will need to get the source code of the library and modify where it tries to access appSettings, hopefully to then pass them in via dependency injection. Commented Feb 27, 2017 at 19:32

1 Answer 1

3

.NET Core as of right now doesn't support System.Configuration, so you can't use the same approach as .NET Framework here. See https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets for guidance on securing sensitive information in configuration during development

Sign up to request clarification or add additional context in comments.

3 Comments

This definitely seems to be the case... but what if I cannot modify the class library that uses System.Configuration? I get that .NET Core has another way of doing it, but the external library is not a .NET Core project, so how am I supposed to pass the values to it safely? Currently I ended up just making the appconfig and excluding it from source. which works I guess, so perhaps that is the answer?
Maybe you're confusing .NET Core with ASP.NET Core? If you have a class library today that is using System.Configuration then that class library isn't targeting .NET Core or .NET Standard (at time of writing) and thus can't be used from a .NET Core project. If you're using ASP.NET Core on .NET Framework then app.config will work like it always did.
okay yes you're right sorry I am confused. the class library is using System.Configuration, and NOT targeting .net core or standard. I'm using a website in ASP.NET core on .NET Framework, and my problem is that app.config works, but ONLY if the appSettings are in the app.config file. I want to move them to an external file (so the appSetting values are not checked into source) but this appears to not work. my problem isn't that configuration doesn't work, it's that I can't put it in a separate file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.