SledgeHammer SledgeHammer - 1 year ago 104
C# Question

Get username in Asp.Net Core Controller

What is the accepted way to get the username of a Asp.Net Core REST service inside of the controller? Assuming the authentication is done by JWT?

I see

contains the username, but is it always going to be in the first claim?

Or is there some better way to access it?

Answer Source

You can use the FindFirst() function on the ClaimsPrincipal class.


// using built in claim types

// using custom claim type