I'm using IdentityServer4 as an OIDC provider and ASP.NET Core 2.0.
I have gone through several posts to ensure that the claims issued by IdentityServer end up in the ClaimsPrincipal (ie Auth Cookie), and have managed to get this working with ClaimsAction filtering.
However my question is this ... When running IdentityServer with the ASP.NET Identity (and EF backing store), how do the ASP.NET Identity properties get mapped to the claims returned by IDS4. By default, IDS4 returns claims like ...
The reason I ask this, is that I would like to map
as IDS4 does not appear to do this by default when requesting the PROFILE scope.
In addition, I would like to ask what would be best-practice for storing user properties. When using the ASP.NET Identity DB, two tables are created ...
So would it be best practice to add additional user properties as ...
As you answered yourself, the extended property should be mapped programmatically.
- If you want to add columns to the
AspNetUsers
table, you extend theIdentityUser
class (e.g.public class MyApplicationUser : IdentityUser
), then add your custom properties (egFirstName
). This essentially changes the model. To ensure that EF writes your model changes to the DB table, you need to extend theIdentityDbContext
class with your newMyApplicationUser
class.- If you want custom claims for the user (e.g.
hair_color
) to be added to theAspNetUserClaims
table, you need to calluserManager.AddClaimAsync()
. You could do this during the registration process or login process with data from the form, or from claims received from external auth providers such as Google, Facebook, Twitter etc.
But, as you can read in my answer to your additional question, I think you should not map them, but make them claims right away.
I found your additional question more interesting: what properties to add to AspNetUsers
and when to AspNetUserClaims
?
After some discussion, I would probably say, that the main consideration would be:
AspNetUsers
) and propagated to claims (when necessary) when creating the principal.AspNetUsersClaims
) right away.Example:
hair_color
is a property entered and changed by the user in this identity-interface, you store it in a strongly typed database column.hair_color
is a property maintained in another application and updated from there, I would store it as a record in the claims-table.In the case I worked on when writing the original answer (below), the user data to be shared was updated indirectly from another (primary) source, but for the authentication details ASP.NET Identity was the primary source. So it resulted in the same distribution, but for a different reason.
As I understand it, the rule of thumb is:
AspNetUsersClaims
.AspNetUsers
), so data for: identification, login, restore, 2-fact, etc. (e.g. username, password, email, phone)Examples:
DateOfBirth
I would add it to AspNetUsersClaims
(unless you intend to use it for authentication/login somehow)accountExpiresDate
or CreatedFromIP
you add it to AspNetUsers
(and extend IdentityUser
)So if you use a UserClaimsPrincipalFactory
to add a user-property to your user-claims, you may just be adding the property to the wrong table!
I actually found your question, while wondering about this myself, yet again. I started using the above rule of thumb, but the question kept coming up, because a lot of examples don't follow this rule.
E.g. Microsoft has an example suggesting to add Name and DOB, which seems to contradict this.
However, OpenID already defines these properties (a.o.) as standard claims in the optional profile
scope: birthdate, name, family_name, given_name, middle_name, nickname, preferred_username
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With