'Add Custom Claims Value From the Database
I need to add a custom Clamis value retrieved from the DB, (i.e. UserID).
This is a blazor hosted app, and that the authentication with Azure AD is already implemented
The value of the claim has to be retrieved from the database and therefore showuld only be read once.
Thanks in advance.
Solution 1:[1]
If you need to add custom claims:
IdentityUser user = await UserManager.FindByIdAsync(Id);
var claim = new Claim(type, value);
var result = await UserManager.AddClaimAsync(user, claim);
To retrieve a claim you shoul use something like:
IdentityUser user = await UserManager.FindByIdAsync(Id);
Claims = await UserManager.GetClaimsAsync(user);
And remember that the claims of AspNetUserClaims are on the cookies when you login via Identity, so you can see it like this in any page:
@if (User.Identity.IsAuthenticated)
{
<p>User: @User.Identity.Name;</p>
<table class="table table-sm">
@foreach (var claim in User.Claims)
{
<tr>
<td>@claim.Type</td>
<td>@claim.Value</td>
</tr>
}
</table>
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Leandro Toloza |
