0

I use Auth0 for authentication and authorization with the backend .Net core for frontend React applications. I am trying to get the logged-in user Id or email using httpContextaccessor in the API for logging purposes. But getting the value like this auth|374634..... Below is the code for the same. I am unsure whether I consider this an Id after this part auth|

public interface IUserIdProvider
{
    string? GetUserId();
}
public class UserIdProvider : IUserIdProvider
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public UserIdProvider(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public string GetUserId()
    {
        var userId = _httpContextAccessor.HttpContext?.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
        return userId ?? string.Empty;
    }
}

I get the information below when I watch the info near userId enter image description here

Please let me know how to get the logged-in user id or user email in API.

Many thanks in advance!!!

kumar425
  • 95
  • 8
  • Would the following help: https://stackoverflow.com/questions/30701006/how-to-get-the-current-logged-in-user-id-in-asp-net-core – bdcoder Jun 29 '23 at 01:07

0 Answers0