Skip to content

Add section for name and role claims configuration for Blazor with Identity Server#18278

Merged
guardrex merged 8 commits intomasterfrom
guardrex/blazor-wasm-security
May 18, 2020
Merged

Add section for name and role claims configuration for Blazor with Identity Server#18278
guardrex merged 8 commits intomasterfrom
guardrex/blazor-wasm-security

Conversation

@guardrex
Copy link
Copy Markdown
Collaborator

@guardrex guardrex commented May 12, 2020

Fixes #17317
Fixes #17649

Thanks @caleblloyd, @AlbertoPa, @hakenr, and @denmitchell.

Internal Review Topic (links to Name and role claim with API authorization section) ... and see the following new section here on Profile Service.

Some of the ask on #17317 was worked on prior PRs. The parts that pertain to Identity Server are on this PR.

The guidance on the PR only supports ONE role per authenticated user. I ran into a problem trying to use the factory pattern for multiple roles. If all you want to do is support one role per user right now, then the PR is fine for now, and we can ignore what follows here 👇.

If you want to know what's failing ........

No factory

👍 RESOLVED ... Single role without a factory works. The app receives a role claim with a string role value. The PR covers this scenario.

Factory pattern

For multiple roles in a role claim ...

"role": [ "role1", "role2" ]

... a factory that parses the JSON works, but this isn't what we want to show. See: #17649 (comment) for @hakenr's approach.

When a factory is implemented without directly parsing JSON (the code for the factory is commented out on the PR to hold it for now) ...

👍 RESOLVED ... When the user has no assigned role (no role claim), account.Roles in the factory is null and so the factory throws. To resolve it, the Roles property in CustomUserAccount is set to an empty string array:

public class CustomUserAccount : RemoteUserAccount
{
    [JsonPropertyName("role")]
    public string[] Roles { get; set; } = new string[0];
}

... let me know if you prefer an alternate approach.

👍 RESOLVED ... When the user has two or more roles ...

"role": [ "role1", "role2" ]

... the factory works and creates a separate role claim for each role. 🎸

💥 BROKEN 💥 ... When the user only has one assigned role in a string (not an array) ...

"role": "role1"

... the factory approach breaks with ...

Unhandled exception rendering component: An exception occurred executing JS interop: The JSON value could not be converted to System.String. Path: $.role[0] | LineNumber: 0 | BytePositionInLine: 181.. See InnerException for more details.
Microsoft.JSInterop.JSException: An exception occurred executing JS interop: The JSON value could not be converted to System.String. Path: $.role[0] | LineNumber: 0 | BytePositionInLine: 181.. See InnerException for more details. --- System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $.role[0] | LineNumber: 0 | BytePositionInLine: 181.
at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue (System.Type propertyType) <0x2fb6d70 + 0x0002c> in :0
at System.Text.Json.JsonPropertyInfo.Read (System.Text.Json.JsonTokenType tokenType, System.Text.Json.ReadStack& state, System.Text.Json.Utf8JsonReader& reader) <0x2fa1238 + 0x00044> in :0
at System.Text.Json.JsonSerializer.HandleValue (System.Text.Json.JsonTokenType tokenType, System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& state) <0x2fa0fc8 + 0x000a0> in :0
at System.Text.Json.JsonSerializer.ReadCore (System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& readStack) <0x2e69f08 + 0x0034c> in :0
at System.Text.Json.JsonSerializer.ReadValueCore (System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonReader& reader, System.Text.Json.ReadStack& readStack) <0x2e68d38 + 0x00548> in :0
at System.Text.Json.JsonSerializer.ReadValueCore (System.Text.Json.Utf8JsonReader& reader, System.Type returnType, System.Text.Json.JsonSerializerOptions options) <0x2e61360 + 0x0003e> in :0
at System.Text.Json.JsonSerializer.Deserialize (System.Text.Json.Utf8JsonReader& reader, System.Type returnType, System.Text.Json.JsonSerializerOptions options) <0x2e611c0 + 0x00024> in :0
at Microsoft.JSInterop.JSRuntime.EndInvokeJS (System.Int64 taskId, System.Boolean succeeded, System.Text.Json.Utf8JsonReader& jsonReader) <0x2e50d40 + 0x00050> in :0

@guardrex guardrex requested a review from a team May 12, 2020 21:19
Comment thread aspnetcore/security/blazor/webassembly/hosted-with-identity-server.md Outdated
@guardrex
Copy link
Copy Markdown
Collaborator Author

UPDATE ... A Javier fix is on the way. I'll institute it for the PR and then merge this by EOD.

@guardrex
Copy link
Copy Markdown
Collaborator Author

FIX IS INhttps://github.com/javiercn/BlazorAuthRoles

I'll get this updated and merged shortly.

@guardrex guardrex merged commit 654d371 into master May 18, 2020
@guardrex guardrex deleted the guardrex/blazor-wasm-security branch May 18, 2020 17:04
@scottaddie scottaddie changed the title Name and role for Blazor with Identity Server Add section for name and role claims configuration for Blazor with Identity Server Jun 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor][Wasm] Using roles with ApiAuthorization + Blazor Additional Section for Adding Roles, or Policies

2 participants