John Davis

John Davis

  • NA
  • 39
  • 751

how to get userid in aspnet core api project?

Jun 8 2022 7:51 PM

previously I was using AspNet Microsoft.AspNet.Identity; to get the current user ID accessing api with following code

string userId = User.Identity.GetUserId();

Now I migrated whole project from AspNet to AspNetCore but how can I get current user ID accessing api?

Tried following code which returned username

var userName = User.FindFirstValue(ClaimTypes.Name); 

I googled and found I have to use the following code to get userID,

var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

but it returned with 204, No Content


Answers (7)