Home > Enterprise >  Setting up User Secrets in dot net core throws exception
Setting up User Secrets in dot net core throws exception

Time:12-22

I am setting up user secrets in secret.json which is throwing error. First I executed this one which was successful -

dotnet user-secrets init

but when I executed dotnet user-secrets set "MySecret" "12345"

received below exception -

System.IO.InvalidDataException: Failed to load configuration from file 'C:\Users\xyz\AppData\Roaming\Microsoft\UserSecrets\1b6c7a6d-8395-4833-9804-221e598cca78\secrets.json'.
 ---> System.FormatException: Could not parse the JSON file.
 ---> System.Text.Json.JsonReaderException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.JsonDocument.Parse(ReadOnlySpan`1 utf8JsonSpan, JsonReaderOptions readerOptions, MetadataDb& database, StackRowStack& stack)
   at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 utf8Json, JsonReaderOptions readerOptions, Byte[] extraRentedArrayPoolBytes, PooledByteBufferWriter extraPooledByteBufferWriter)
   at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 json, JsonDocumentOptions options)
   at System.Text.Json.JsonDocument.Parse(String json, JsonDocumentOptions options)
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser.ParseStream(Stream input)
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser.Parse(Stream input)
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.HandleException(ExceptionDispatchInfo info)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
   at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at Microsoft.Extensions.SecretManager.Tools.Internal.SecretsStore.Load(String userSecretsId)
   at Microsoft.Extensions.SecretManager.Tools.Internal.SecretsStore..ctor(String userSecretsId, IReporter reporter)
   at Microsoft.Extensions.SecretManager.Tools.Program.RunInternal(String[] args)
   at Microsoft.Extensions.SecretManager.Tools.Program.TryRun(String[] args, Int32& returnCode)
Command failed : Failed to load configuration from file 'C:\Users\xyz\AppData\Roaming\Microsoft\UserSecrets\1b6c7a6d-8395-4833-9804-221e598cca78\secrets.json'.

CodePudding user response:

  • I am able to run and get the UserSecretsId and set the secret id without any issue.
  • Make sure you are running the command in the root directory of the project.

enter image description here

  • Tried with .Net Core 3,6 and 7 as well.

Core 6 :

dotnet user-secrets init

enter image description here

dotnet user-secrets set "MyKey:ServiceApiKey" "Generated UserSecretsId"

enter image description here

Right click on the project folder => Manage User Secrets to open the secrets.json file.

enter image description here

My secrets.json :

{
  "MyKey:ServiceApiKey": "Generated UserSecretsId"
}

Able to retrieve the key in Program.cs.

enter image description here

  • Related