I am using the ASP.NET Identity library. I added a claim, and I do not get it back when I ask for all of a user's claims. I even restarted my application in case it doesn't update right away. Still no luck.
Code to get the claims:
[CascadingParameter]
private Task<AuthenticationState> AuthenticationStateTask { get; set; } = default!;
private async Task InitializeSchedulerData()
{
var authenticationState = await AuthenticationStateTask;
var claims = authenticationState.User.Claims.ToList();
This gets me (from the debugger):
Count = 5
{http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: 091a0828-6cc5-4cee-bf3a-a2348872de94}
{http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: colorado@thielen.com}
{http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress: colorado@thielen.com}
{AspNet.Identity.SecurityStamp: JKNDXBICJOPUYON4DIEZK2OD4RODU5NZ}
{Admin: State:Colorado}
While in the database, the table AspNetUserClaims is:
Id UserId ClaimType ClaimValue
1 091a0828-6cc5-4cee-bf3a-a2348872de94 Admin State:Colorado
2 c3a84b03-c3f3-423d-82cf-2c885aca2233 Admin Campaign:Hick4Colorado
3 12e37c4f-149e-466b-875a-1965a575f5c9 Admin Campaign:Frisch4Colorado
4 487e9d8a-58d4-49f8-8100-3e014f914012 Director Campaign:Frisch4Colorado
5 a00342e5-b7b7-4426-8868-a1c0ff82f96d Admin *
10 034225c9-e6ff-408e-be77-f5e292078a0a Executive State:Alabama
11 77325df9-6147-46e2-a1c3-0bf0e32ba710 Executive County:Alabama_Baldwin
12 091a0828-6cc5-4cee-bf3a-a2348872de94 Executive Campaign:Hick4Colorado
Any idea why it isn't finding the new entry? The UserId entry for both matches.
Update: I exited and restarted Visual Studio. Now it does show up. But why is that required? I need it to show up immediately. How do I accomplish that?
Update 2: It looks like it needs to have SignInManager.RefreshSignInAsync(user);
called to update the user/claims held in a cookie (which explains why exiting VS also resolved it). The problem is, you can't call SignInManager in blazor code.
Is there any downside to, when I need to do this, call an ASP.NET MVC page (placed with the other Identity MVC pages) that in OnGet() calls SignInManager.RefreshSignInAsync(user);
and then redirects to a url I pass in the call to this page?
I know we're supposed to use OnPost(). But there's no way to redirect to a page and have it call OnPost. So placing this in OnGet() seems the only option.
What problems do I cause or security holes do I create if I do this in an OnGet()?