Merged
Conversation
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
halter73
reviewed
Feb 15, 2024
Member
halter73
left a comment
There was a problem hiding this comment.
Nice changes. I really like seeding some data for development.
Ugh! ... that's ☝️ probably more rotten 🙈 RexHaqs!™ code 🙈. I couldn't get a Claim[] array to play nicely with TypedResults.Json because Claim doesn't have a parameterless ctor and the JSON serializer usually flakes out in a ☠️ loop
I like this RexHaq™. I've done stuff very similar to this because you cannot simply JSON serialize a Claim. I also like the usage of anonymous types.
Co-authored-by: Stephen Halter <halter73@gmail.com>
|
Just dropping by to say that I'm immensely grateful for the Roles example and this sample project in general. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses dotnet/AspNetCore.Docs#31045
Probably a fancier and more performant way to do this 🙈, but here's something to get us rolling at least for discussion.
Everything I show below is on the PR and ✨ Just Works!™ ✨.
Backend app
First of all, I think it's a good idea to have seeded data for learning and testing with role claims, AND I loathe having to re-register a test user over and over ... and over 😠. I place a
SeedDataclass in here to take care of both. It creates a user, Bob 🤠, with two role claims (Administrator,Manager). We'll get seeded right after the app is built ...IdentityUserhas nothing to hold roles, so I keptAppUser. We were going to shed it and useIdentityUser, but it's just as well that I didn't because we need it for roles. I addedIEnumerable<IdentityRole>toAppUser...Add roles to the Identity bits ...
We need some kind of endpoint for the frontend to tap for the user's roles, so I went with the following Minimal API approach ...
Ugh! ... that's ☝️ probably more rotten 🙈 RexHaqs!™ code 🙈. I couldn't get a
Claim[]array to play nicely withTypedResults.JsonbecauseClaimdoesn't have a parameterless ctor and the JSON serializer usually flakes out in a ☠️ loop, perhaps because the claim'sSubjecthas a claims collection. I'm not familiar with source generators/reflection concepts inSystem.Text.Jsonserialization, but I did have a little luck with ...However, that only (apparently) sends down the
Administratorrole claim. It doesn't send the other role claim forManager. The simplest way for me to solve this was to select (.Select) what's needed from the claims into anIEnumerableand run the JSON serializer on that to send the role claims.The PU is welcome to fix this with a proper source generator and make it work with a normal
Claim[]array (ToArray). The silly 🦖 will watch and learn!BlazorWasmAuth app
Added two pages: One requires a
Managerrole claim, and the other requires anEditorrole claim. I added links to them to theNavMenu, and the links show up when merely authenticated. That's by-design because we want to demo that theEditorpage can't be reached by Bob 🤠 the test user with onlyAdministratorandManagerrole claims.The action takes place in the
CookieAuthenticationStateProvider'sGetAuthenticationStateAsyncmethod. I add the following to make claims out of the role claims that come down from tapping the/rolesendpoint of the Backend app.First, something to receive the claims. Again, trying to use a
Claim[]array flakes out the JSON serializer without source gen. Using a custom class is the low-hanging-🍎 approach. ... and again, the PU is welcome to fix this to just deserialize with aClaim[]array.... and just before the
ClaimsIdentityis created inGetAuthenticationStateAsync...The role claims JSON (formatted for display here) returned by the
/rolesendpoint when run locally for the example:[ { "issuer" : "LOCAL AUTHORITY", "originalIssuer" : "LOCAL AUTHORITY", "name" : "bob@contoso.com", "type" : "http://schemas.microsoft.com/ws/2008/06/identity/claims/role", "value" : "Administrator", "valueType" : "http://www.w3.org/2001/XMLSchema#string" }, { "issuer" : "LOCAL AUTHORITY", "originalIssuer" : "LOCAL AUTHORITY", "name" : "bob@contoso.com", "type" : "http://schemas.microsoft.com/ws/2008/06/identity/claims/role", "value" : "Manager", "valueType" : "http://www.w3.org/2001/XMLSchema#string" } ]