Open Closed

Getting a user when creating a new Claim #9961

sduffy created

.net core MVC 9.x

I am looking for some assistance with a problem I have with creating a new claim on a ClaimPrincipal. As a quick background to what I am doing, we are enhancing the organization unit (OU) functionality in such a way that a user can only access OUs that they belong to. I have followed this guide:\How-To\add-custom-data-filter-ef-core

However the guide assumes that a User HAS an OU, not is IN one or many OUs.

It is no problem to modifying the the UserClaimsPrincipalFactory::CreateAsync method to add a claim to describe the ID's of the OUs, but the problem I am having is ensuring the the User record that is passed in has the OrganizationUnits collection hydrated. If the user is not hydrated, I cannot add the claim.

I can of course load the OU's for that current user each time the CreateAsync method is called, but it seems unnecessary making an extra request to the database.

I would prefer to intercept the Repository in the UserStore and add an 'include' somewhere central.

The only way I have been able to do this is to override all the 'Find' methods in the UserStore and add in the include statement. This seems really rubbish.

Does anyone have any suggestions?

1 Answer(s)
  • 0
    ismcagdas created
    Support Team

    Hi @sduffy

    Altough it is seems like an extra request to the databsae, I think best way is to load OUs in CreateAsync method of UserClaimsPrincipalFactory.