I have a use case where a user of our .NET Core API can belong to multiple "organizations" and have different roles within those orgs.
For example, if they POST to a Location/Create endpoint on behalf of OrganizationId = 1, they should have Admin privileges. However, if they try to access that endpoint on behalf of OrganizationId = 2, they should have only basic user privileges. The user can perform these actions without logging out and logging in again.
I would like to write some middleware to make a DB call and retrieve the role for the user in the organization they are attempting to modify, then use the .NET Core Role syntax on controllers to restrict access at that level.
I have not been able to find an example of a case where middleware modifies the users role before the request pipeline reaches the controller action, on each request. Does anyone have experience with a similar authentication scheme?