Details

Get User Info from HttpContext

Get various user information from the context object

public class CurrentUser : ICurrentUser
{
    private readonly IHttpContextAccessor _httpContextAccessor;

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

    public IEnumerable<Claim>? GetUserClaims()
    {
        return _httpContextAccessor?.HttpContext?.User.Claims;
    }

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

    public string? GetUserEmail()
    {
        return _httpContextAccessor?.HttpContext?.User
            .FindFirst(ClaimTypes.Email)?.Value;
    }

    public string? GetUserIpAddress()
    {
        return _httpContextAccessor?.HttpContext
            ?.Connection?.RemoteIpAddress?.ToString();
    }

    public bool IsAuthenticated()
    {
        return _httpContextAccessor?.HttpContext
            ?.User.Identity?.IsAuthenticated ?? false;
    }
}

Add more as necessary.