I have a Web Api 2 endpoint which I am trying to secure with a custom Authorize attribute. Inside of that attribute I check if the user has the necessary roles like:
if (!user.HasRoleInInstitution(institutionId, Role.SomeRole))
{
filterContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
return;
}
The problem I am having is the institutionId. The endpoint should be accessible in institution context in which every user can have different roles in different institutions so the check should check if the user has roles in the given institution the user is accessing.
I could store the currently accessed institutionId in Session like:
var session = HttpContext.Current.Session;
This however:
A) Is against REST principles as the endpoint should be stateless
B) Sessions access tend to take long so this introduces a performance overhead not tolerable in scenarios like Authorization.
I could also pass the institutionId from the client on every request and then use RouteData to retrieve it in the Attribute like:
var institutionId = filterContext.ControllerContext.RouteData["institutionId"];
The problem with this approach is:
A) If another developer uses "someInstitutionId" instead of "institutionId" as route parameter then this check will break
B) The passing of the institutionId on every request is a horrible waste of bandwidth
Finally I could just add a currently accesed institutionId prop to user or pass around cookies but that forces the user to relog every time he wants to use a different institution and he cannot have multiple instances of the application open for different institutions which is a hard requirement.
What would be the best approach for scenarios like that?