Reputation: 2143
I am writing a service that accesses information about the current user.
Currently I pass the User property from the controller in through a parameter into my logic class and working with the ClaimsPrincipal that way.
I know there's usually some way to access the current user globally (in previous versions using HttpContext.Current.User, for example), so what is the current equivalent in MVC 6?
The answer to this question explains that ClaimsPrincipal.Current is not thread safe and therefore not a useful replacement for HttpContext.Current.User
Upvotes: 3
Views: 1600
Reputation: 2143
According to this answer, a way to do this is with using DI to inject the HttpContextAccessor into your class, and then you can access the current user through _httpContextAccessor.HttpContext.User
I won't go into the specifics of implementation as his answer covers it pretty well.
Since that question was referring to HttpContext in general, and not specifically the current user, I feel that my question is still worth leaving in place for reference.
Upvotes: 6