It is possible to bind actions' parameters via:
[FromBody]
Request body[FromForm]
Form data in the request body[FromHeader]
Request header[FromQuery]
Request query string parameter[FromRoute]
Route data from the current request[FromServices]
I often need to extract something from a JWT, almost always the id
(primary key). So I do this (ignore error checking for now):
var id = int.Parse(base.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
It would be great if I could put that into an attribute binder that would work like this:
public IActionResult doStuff([FromBody] MyModel model, [FromJwt] int id) {
// id works automatically
}
Or maybe [FromJwtId]
instead to make it simpler.
Is such a thing possible?
I think it is possible to create such attributes using HttpParameterBinding
.
Microsoft has a tutorial on that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With