Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

c# - .net 5, secrets.json, appsettings.json and Azure Application Settings

I have a .net (core) 5 application which I am trying to setup so that sensitive values are held in secrets.json file, and when the application is published to Azure I can then use the Application settings area of the app service to provide the values from here instead.

However, I am unable to use both at the same time.

In my secrets.json file I have:

{
   "UserName": "LocalUserName"
}

In my appsettings.json file I have:

{
   "UserName": ""
}

As I understand that I need the same structure, but without the value assigned to the key.

In my Startup.cs file I can reference: Username = Configuration["Username"] Which only gets the blank key in the appsettings.json file instead of the secrets.json file

In the Application settings area I am setting:

{
  "name": "Username",
  "value": "PublishedUserName",
  "slotSetting": false
}

But is not picked up.

I have seen references to use ConfigurationManager.AppSettings["Username"]; instead. But if I use this, then this doesn't reference either of my json files when debugging.

I'm new to using .net core, so I'm struggling. In essence what I'm trying to do is replicate what I would have with an appsettingssecrets.config file outside of the application in a .net framework solution. Which I can then reference the same keys in Azure and provide published values from there.

question from:https://stackoverflow.com/questions/66066865/net-5-secrets-json-appsettings-json-and-azure-application-settings

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a tutorial about how to store appsettings in secrets.json.

You could check things below:

  1. If you configure UserSecretsId in your project file?
  2. Register the user secrets configuration source.
  3. Read the secret via the Configuration API in correct format.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...