Why not turn it into a lazy controller property?
Its nice because now you don't get the user out of the cache if you are not using it.
private User _user;
public User user { get { return _user ?? (_user = UserCache.Get(this.Request.Properties["token"])); } }
Call initializer on constructor
About SetInfo().
I Suggest to move this code to the constructor or somewhere else (depending on the contents of this function). The Controller is created after the Router has done its work. By the IControllerFactory. You can extend the DefaultControllerFactory. This class is responsible for creating controller instances. (make sure to register it in IServiceCollection, otherwise your class is not the one that is resolved.
In your case i think its best to rethink your architecture here and focus on being as stateless as possible and not using one of the anti-patterns described below.
Information MVC request pipeline
This is how the request pipeline works in mvc (and still works in aspnetCore).
image from dotnetCurry
Accessing user earlier in the pipeline
If you want to access the user somehwere earlier in the request pipeline, i would suggest:
- @dabouls answer: Add middle-ware to inject it in Http.Items
- Create a UserRepository class and register it to request scope in Ioc container. + use the HttpContextAccessor, but accessing HttpContext.Current is generally an anti-pattern and is discouraged.
SetInfo()doing, it make sense if the entiretokenhandler can be handled n middleware for pipeline processing approach.