NET 3.1 core project. I have registered the application in azure AD and I have made configurations to receive roles in JWT token. I have roles like Global Admin, Regional Admin and User. currently In .Net core I have APIS and APIS will send and receive data from database. Most of the APIS will be accessed by all the roles as below.
[Authorize(Roles = "GlobalAdmin,RegionAdmin,User")]
Now coming to DAL or data later part, I need to retrieve data based on the roles. For example,
If the Role is GlobalAdmin then return all the data If the Role is RegionalAdmin then return region specific data If the role is user then return only his data.
I have below implentation in DAL layer
var returnData = await _mysamplerepository.GetAsync(x => x.id == request.rId, null, x =>
x.ReferenceSystem).ConfigureAwait(false);
This is some sample query before RBAC. Now I want to return the data based on users roles. I am thinking something like this
if(role == "GlobalAdmin")
Then return all the data(Similar to select * from)
if(role == "RegionAdmin")
Then return data for that region(similar to select * from table name where region = some region)
if(role == "user")
Then return only his data(similar to select * from table where createdby = 'currentuser')
Above is dummy code used to explain the logic what I have in my mind. I am wondering this is the right approach or any other formal approaches available to handle this kind of situations.
Now Another question I have in my mind is In JWT token I have roles. When I apply
[Authorize(Roles = "GlobalAdmin,RegionAdmin,User")]
automatically .Net will read roles from token and do the authorization. Now If I want to pass role details down to DAL, I need to read roles and pass it to DAL. So I am just wondering how can we read grammatically roles from JWT token and pass it down to DAL layer to apply some business logic based on the roles.
Can someone help me regarding this? Any help would be appreciated. Thanks