7
Answers

how to get userid in aspnet core api project?

John Davis

John Davis

2y
782
1

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)