1
I recently asked a question in stackoverflow in English, but I may have expressed myself badly and I did not get the answer I wanted. Link in English.
I have 3 different applications: Identityserver who is my identity provider; Teaching.MVC which is the frontend in Asp.net mvc; and Teaching.Api.. The latter is the Api itself. The authentication process, Claims definition, scopes and everything else is up and running. It turns out: in the Api I need to define Api-specific Claims. For example:
Identityserver: Authenticates the user and provides the access_token to ensure access from Mvc to Api. This token contains the Profile, Email, User Id and everything that refers to the user. This application only has access to the Identities database.
Teaching.: Log in via Identityserver and save the access_token to access the Api. All this process is described in the Identityserver4 documentation.
Teaching.Api.: Here is the key to the question. The Token the api receives contains the user-related Claims: Id, profile, email... But the Api needs to restrict certain users' access to certain features. The Api has access only to the application database, and does not have access to the identity database. So in the Api, through the User Id, I search if it has registered schools. If so, I should assign it to the "Director" Claim. This same user can also be a student, so I search the database if he has "enrollments". If he has, I assign him to Claim "Student". But I have no idea how I do it at the Api level.
Summary: According to the Claims received in the Api by the access-token, I have to generate new Claims, which are specifically from the Api, and save them so that they can be accessed by context and validate the Policies. All this in the Api.
If anyone knows a better way to do this access control, suggestions are always welcome.
Thanks for the reply Renan. Despite being an old problem I had, it is always good to see other perspectives. As Marcelo Dias said, and as I also think, the identity server should have the responsibility to only identify the user and say who it is. Thus, the API should give this user access to what he is entitled to. Because MVC is only a view layer, as well as a mobile app, which can also access the API, it is not up to it to the permissions process, since I should replicate the permissions logic for other possible user interfaces. With the API I centralize.
– Raul Medeiros
I suggested controlling this logic in a specific application with Identityserver4 because in a scenario where you have 10 API’s instead of you having to implement access rules in each of them, you would only implement in the application with Identityserver4. Then the MVC application goes in the Identityserver4 application, this Identityserver4 application configures the Claims and passes the token to the MVC application which in turn can then access the API. But it is an option/suggestion among others that solves the problem :-)
– Renan