Details
Get User Id from Claims
To get the user id from claims, create a Utilities
folder
Create a UserContextService
class
Inject the IHttpContextAccessor
in the constructor and create a
private backing field for it
Create a GetUserId
method to get the user id from the claims
public class UserContextService
{
private readonly IHttpContextAccessor _httpContextAccessor;
public UserContextService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public string? GetUserId()
{
var userId = _httpContextAccessor.HttpContext?.User?
.FindFirstValue(ClaimTypes.NameIdentifier);
return userId;
}
}