.net core user secret file for storing the connection string

Noam Rinat asked on January 15, 2021 19:09

I would like to use the user setting file to store the CMSConnectionString. It seems to be unsupported by Kentico out-of-the-box. If I move the connection string to the user secrets file I get the following error:

CMS.DataEngine.ApplicationInitException: 'Cannot access the database specified by the 'CMSConnectionString' connection string. Please install the database externally and set a correct connection string.

How can I get it done in Kentico, or is there an alternative that's considered best practice by Kentico?

Correct Answer

Noam Rinat answered on March 8, 2021 22:38

I am using IIS locally which explains why the user secrets cannot be read, as IIS is usig the application pool identity.

0 votesVote for this answer Unmark Correct answer

Recent Answers

Radek Macalik answered on January 18, 2021 10:39

Hello Noam.

It should be possible to configure this in Kentico as this task should only depend on the correct configuration of the .NET Core app. As far as I know, we do not forbid or limit this task in any way.

According to Microsoft documentation below, the configuration must be read via IConfiguration and the framework will ensure the rest, i.e. returning correct secret or the web.config storage value. This is how we have implemented it in our API (AppsettingsService.cs which reads it via IConfioguration), so the app should handle it, supposing the configuration itself is correct.

Could you please consult this article and confirm that you did configure it accordingly? https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-3.1&tabs=windows#enable-secret-storage

You may also check this one regarding host builder setup: https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-3.1&tabs=windows#register-the-user-secrets-configuration-source

Should you consult those links and found no misconfiguration in your app, could you please share with us more details of your configuration including some screenshots, so we can check it? Thank you.

Best regards, Radek Macalík Kentico support

0 votesVote for this answer Mark as a Correct answer

Noam Rinat answered on February 18, 2021 16:46 (last edited on February 18, 2021 21:27)

Hi Radek

Thank you for your answer. I forgot to mention, we use .NET 5 and Kentico version v13.0.8. I followed the documentation (.NET 5 version) and I still can't figure out what I'm doing wrong.

I ran the following commands: * dotnet user-secrets init * dotnet user-secrets set "*** see CMSConnectionString setting below ***"

This is my secrets.json structure: { "CacheConnection": "---redis.cache.windows.net,abortConnect=false,ssl=true,password=---", "ConnectionStrings:CMSConnectionString": "Data Source=---.database.windows.net,1433;Initial Catalog=---;Integrated Security=False;Persist Security Info=False;User ID=---;Password=---;Connect Timeout=60;Encrypt=False;Current Language=English;" }

The project file contains a reference to my secrets ID:

I am using the default builder (I a not calling builder.AddUserSecrets): public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup Startup(); });

The following Kentico features are used: features.UsePageBuilder(); features.UsePageRouting();

In the MS documentation they show you how to read the configuration, which I am not trying to do as I expect it is already done (unsuccessfully) by Kentico.

If I take the connection string out of the secrets file and put it in the appsettings file everything works as expected.

I know that Kentico is not officially tested with .NET 5. Could that be the issue? What other information can I provide?

Thank you!


0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.