If you are in a controller context, you can access the current authenticated User. Take an article such as Get the current user, within an ApiController action, without passing the userID as a parameter .
However, if my controller calls a service (same assembly), but here I don't have the controller context.
What is the best way to actually get the authenticated user?
You can create an intermediate service to provide that functionality
and inject it into the dependent service context
Finally make sure abstraction and implementation are registered with DI container in composition root of main application so that when service is injected into controller it would also be able to access current request's user.
When Identity framework authenticates a user the user principal is then set for the current context.
If hosted in IIS you can tap into
HttpContext
to access the user like in the example provided earlier. MVC and Web API basically do something similar to populateController.User
andApiController.User
.If self hosting there are other ways to access it.
That fact is that once authenticated, the user is available. Encapsulate it behind an abstraction and you can injected where ever it is needed outside of a controller.
Asp.net Core introduced something similar
IHttpContextAccessor
which allowed service classes to access the currentHttpContext
out side of controllers