-
Notifications
You must be signed in to change notification settings - Fork 660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add roles to WASM+Identity sample app #140
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
8.0/BlazorWebAssemblyStandaloneWithIdentity/Backend/SeedData.cs
Outdated
Show resolved
Hide resolved
8.0/BlazorWebAssemblyStandaloneWithIdentity/Backend/SeedData.cs
Outdated
Show resolved
Hide resolved
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. |
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
SeedData
class 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 ...IdentityUser
has 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.Json
becauseClaim
doesn't have a parameterless ctor and the JSON serializer usually flakes out in a ☠️ loop, perhaps because the claim'sSubject
has a claims collection. I'm not familiar with source generators/reflection concepts inSystem.Text.Json
serialization, but I did have a little luck with ...However, that only (apparently) sends down the
Administrator
role 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 anIEnumerable
and 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
Manager
role claim, and the other requires anEditor
role 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 theEditor
page can't be reached by Bob 🤠 the test user with onlyAdministrator
andManager
role claims.The action takes place in the
CookieAuthenticationStateProvider
'sGetAuthenticationStateAsync
method. I add the following to make claims out of the role claims that come down from tapping the/roles
endpoint 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
ClaimsIdentity
is created inGetAuthenticationStateAsync
...The role claims JSON (formatted for display here) returned by the
/roles
endpoint when run locally for the example: