0

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'.
Harshitha
  • 3,784
  • 2
  • 4
  • 9
F11
  • 3,703
  • 12
  • 49
  • 83
  • What does `dotnet user-secrets list` show? – Stephen Gilboy Dec 20 '22 at 21:32
  • 2
    Looks like your `secrets.json` is not valid json. Maybe there is a bug in `dotnet user-secrets`?. Good news is that you can use any text editor and fix the `secrets.json` file . – Christian Held Dec 20 '22 at 21:36
  • 1
    Those commands work for me. The only way I was able to break them was by editing the JSON and making it invalid. Perhaps you ran those commands earlier, edited the JSON, then tried again another day? Only other thing I can think of would be you using a buggy version of `dotnet` (not likely). – Kit Dec 20 '22 at 21:44
  • Hi @F11, It works fine in my project also. Could you pls share the steps to reproduce your issue, how is your environment and where do you run this command, what is the version of asp.net core? – Rena Dec 21 '22 at 07:24
  • I have made some manual changes in Secret.JSON which was causing issue – F11 Dec 21 '22 at 13:28

1 Answers1

0
  • 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

Harshitha
  • 3,784
  • 2
  • 4
  • 9