dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.

Home Page:https://asp.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dotnet user-jwts create throws exception if user secrets JSON contains objects

martincostello opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

If a secrets.json file contains structured JSON, similar to that shown below, rather than key-value pairs indicating sections with the : delimiter then an exception is thrown when dotnet user-jwts create is run.

{
  "Foo": {
    "Bar": "baz"
  }
}
❯ dotnet user-jwts create
Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $.Foo | LineNumber: 1 | BytePositionInLine: 10.
 ---> System.InvalidOperationException: Cannot get the value of a token type 'StartObject' as a string.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_ExpectedString(JsonTokenType tokenType)
   at System.Text.Json.Utf8JsonReader.GetString()
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonDictionaryConverter`3.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TDictionary& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[TValue](JsonReaderState& readerState, Boolean isFinalBlock, ReadOnlySpan`1 buffer, JsonSerializerOptions options, ReadStack& state, JsonConverter converterBase)
   at System.Text.Json.JsonSerializer.ContinueDeserialize[TValue](ReadBufferState& bufferState, JsonReaderState& jsonReaderState, ReadStack& readStack, JsonConverter converter, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.ReadAll[TValue](Stream utf8Json, JsonTypeInfo jsonTypeInfo)
   at Microsoft.AspNetCore.Authentication.JwtBearer.Tools.DevJwtCliHelpers.CreateSigningKeyMaterial(String userSecretsId, Boolean reset)
   at Microsoft.AspNetCore.Authentication.JwtBearer.Tools.DevJwtCliHelpers.GetOrCreateSigningKeyMaterial(String userSecretsId)
   at Microsoft.AspNetCore.Authentication.JwtBearer.Tools.CreateCommand.Execute(IReporter reporter, String projectPath, JwtCreatorOptions options)
   at Microsoft.AspNetCore.Authentication.JwtBearer.Tools.CreateCommand.<>c__DisplayClass2_0.<Register>b__1()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.AspNetCore.Authentication.JwtBearer.Tools.Program.Run(String[] args)
   at Microsoft.AspNetCore.Authentication.JwtBearer.Tools.Program.Main(String[] args)

Expected Behavior

The tool handles such settings, either by ignoring them, or building up the display value of the setting (e.g. Foo:Bar) from the nested objects and does not throw an exception.

Steps To Reproduce

  1. Create a new ASP.NET Core web application (e.g. dotnet new webapi)
  2. Using a tool such as Visual Studio's Manage User Secrets right-click context command open the secrets.json file for the application and add the following JSON:
{
  "Foo": {
    "Bar": "baz"
  }
}
  1. Run dotnet user-jwts create from a terminal in the application directory.

Exceptions (if any)

Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $.Foo | LineNumber: 1 | BytePositionInLine: 10.
 ---> System.InvalidOperationException: Cannot get the value of a token type 'StartObject' as a string.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_ExpectedString(JsonTokenType tokenType)
   at System.Text.Json.Utf8JsonReader.GetString()
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonDictionaryConverter`3.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TDictionary& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[TValue](JsonReaderState& readerState, Boolean isFinalBlock, ReadOnlySpan`1 buffer, JsonSerializerOptions options, ReadStack& state, JsonConverter converterBase)
   at System.Text.Json.JsonSerializer.ContinueDeserialize[TValue](ReadBufferState& bufferState, JsonReaderState& jsonReaderState, ReadStack& readStack, JsonConverter converter, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.ReadAll[TValue](Stream utf8Json, JsonTypeInfo jsonTypeInfo)
   at Microsoft.AspNetCore.Authentication.JwtBearer.Tools.DevJwtCliHelpers.CreateSigningKeyMaterial(String userSecretsId, Boolean reset)
   at Microsoft.AspNetCore.Authentication.JwtBearer.Tools.DevJwtCliHelpers.GetOrCreateSigningKeyMaterial(String userSecretsId)
   at Microsoft.AspNetCore.Authentication.JwtBearer.Tools.CreateCommand.Execute(IReporter reporter, String projectPath, JwtCreatorOptions options)
   at Microsoft.AspNetCore.Authentication.JwtBearer.Tools.CreateCommand.<>c__DisplayClass2_0.<Register>b__1()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.AspNetCore.Authentication.JwtBearer.Tools.Program.Run(String[] args)
   at Microsoft.AspNetCore.Authentication.JwtBearer.Tools.Program.Main(String[] args)

.NET Version

7.0.100-preview.5.22307.18

Anything else?

No response