in the WebApi I can get the current user using AspIdentity that way
User.Identity.GetUserId<long>();
However, The service Layer doesn't know about the AspIdentity. And I don't want to passe the current user as paramettre for all the methodes.
In other word, what pattern I can use to get the current user wherever I want.
The reason for this is to log all the activity the current user is doing in the application.
Finally this is how I implemented it.
In the Framework Layer (a comun independent project) I added this:
public interface ICurrentContextProvider<T>
{
T GetCurrentUser();
}
In the API I Added the implementation of this interface, something like :
public ApplicationUser GetCurrentUser()
{
return _usersService.FindByIdAsync(Convert.ToInt64(HttpContext.Current.User.Identity.GetUserId())).Result;
}
And As the all other layers know about the Framework Layer I can call it anywhere without any reference to AspIdentity and without passing the Current user to all my functions.
Note: I make it a generic because of the following: 1-Framework (common layer) doesn't know about application user 2-I can get any other type from the context like the ApplicationUserId which is Long.