I think the idea is not to cancel the middleware call from inside the middleware, if you call some asynchronous task that accepts the cancel token, then you can create it and pass it to what you are calling from the inside.
A common scenario would be to cancel the task if the request was aborted, so you could create a token like this:
CancellationToken CancellationToken => context?.RequestAborted ?? CancellationToken.None;
and then call some async service, for example, to get data or request db, if the request is canceled, then a cancellation request should occur
source share