How to get a User ID in Asp.Net Core 3.1 (ClaimPrincipal) in a Service Repository

Question: How do you get user id in .net core MVC from a service repository?

Please Login to see the rest of the answer

Answer: The User's Id can be obtained from .net core application in several ways, one of them is by getting User from an HttpContext:

  1. Create an Interface with a name "IUserUtility" that has a function defined as "GetUserID()"
    - This function can return a Task of a "String" if you like.
  2. Create a Mock Repository that implements the "IUserUtility"
    - In the Mock Repository, create a constructor that will utilize "Dependency Injection".
    - Dependency Injection will help you get access to Asp.Net Core inbuilt API's and other Services (User-defined Interfaces registered as Services in Startup.cs File).
    - In the Mock Repository, Inject HttpContext in the Constructor as well as define a private property on top of the Constructor
         - Defining a private read-only helps your application not to create a memory consuming variable that sometimes hangs even when the Service is not being utilized. Creating private read-only will allocate small portion of memory to that private variable and return the memory to the application when the variable is not in use.
    - wire the private HttpContext _httpContext with the HttpContext httpContext injected in the Constructor, this will input the httpContext Service coming from the Application Startup.cs Services into a private readonly _httpContext.
    - After implementing the UserUtility Interface go ahead and write your code in the GetUserID() function. This function will contain code that gets User ID and returns a string or an object containing the Current Logged in User ID.
  3. See the Code below for reference:
    //This an Interface
    namespace YourProjectName.FirstLevelFolder.FinalFolderYourInterfaceClassIsLocated
        public interface IUserUtility
            Task<string> GetUserID();
    //This is a Service Mock Repository Class that implements IUserUtility Interface
    namespace ProjectName.FolderName
        public class UserUtilityRepository : IUserUtility
            private readonly SignInManager<AnotherUserMockRepositoryClass> _signInManager;
            private readonly HttpContext _contextRequest;
            public UserUtilityRepository(SignInManager<AnotherUserMockRepositoryClass> signInManager, HttpContext contextRequest)
                _signInManager = signInManager;
                _contextRequest = contextRequest;
            public async Task<string> GetUserID()
                ClaimsPrincipal principal = _contextRequest.User as ClaimsPrincipal;
               string UserID = _signInManager.UserManager.GetUserId(principal);
                return UserID;

    [NB]: Errors: You might an error that says:

    An unhandled exception occurred while processing the request.

    InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.HttpContext' while attempting to activate 'YouProject.Controllers.YourController'.

    Write an Article
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

© 2022 - ErnesTech - Privacy