It is a general requirement that, once the user is validated and received token and redirected to actual API to fetch or post the data. Here, if we want to fetch any information about a logged In user from API, we need to send userid/username to the API. But, in our case, in practical when we hit the Authentication server, we receive only access-token to calling front-end and we cannot able to decode this token to fetch user information from that.

Instead, we need to send this token as a Bearer token to the next request to API to fetch the user details.  In detail, once API finds Bearer token, it will validate that token and prepares the ClaimsPrincipal object (Hope ASP.NET devs aware of it). When we call this action, there we can directly call User object.

Note: You should decorate your Controller with [Authorize] attribute to fill that User object.

Following is the code where we can fetch the username from the User object.

/// <summary>
/// Returns the login user details
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("GetUserDetails")]
public async Task<IActionResult> GetUserDetails()
{
    try
    {
        var email = User.Claims.FirstOrDefault(c => c.Type == "sub").Value;
        var user =  _authBusiness.GetUser(email);
        return Ok(new Response()
        {
            Status = true,
            Data = new  {
                user.FirstName,
                user.LastName,
                user.EmailAddress,
                user.UserTypeId
            }
        });
    }
    catch (Exception ex)
    {
        return Ok(new Response()
        {
            Status = false,
            Message = ex.Message
        });
    }
    
}

Hope it Might helpful for you.

Happy Coding 🙂