Access user claims + set attributes of called method in Global Query filter
Hello! I was wondering whether it is possible to access what API Controller & what method is called in the IHttpContextAccessor (when using it in a global query filter in the app its DbContext).
+ As well as all the attributes which are set above those controllers/methods. Both are very needed.
I know I can access the users claims, but I need to know if and how I can access the method with its attributes of the request.
If not with the ihttpcontextaccessor, is there any other way of doing this?
(Basically: I would like to access my user’s claims (using the httpcontext). With that, I would like to check the [Attributes] set on the called method. Then I would like to filter if certain properties in both match, and based on that outcome, apply certain id(s) to filter for in the request.
That’s also why I need it in a global query filter. I dont want to copypaste this code everywhere.)
1 Reply
Ok I have an idea which may work...... but it's kind of dirty.
Im still searching and looking for the best solution.
But I think, in the attribute i want to target, i can probaply append something to the context of the attribute. Which is then appended to the httpcontext of the request everywhere, and based on that i can do my checks. But I'll continue searching if theres a better way (not even sure if this is possibly, should be in theory..)
Solved! Did it with httpcontext.items, i set the properties there in my attribute (properties that are set on the attribute) using a key value pair.
Next, I can access it in the context of the dbcontext.
Attribute is called first since you want to authorize etc before you query. So the execution order is good too.