I have asp.net core Identity and I want if it is possible to retrieve username of any user using the identity. I know we can subtract Id from the current user as (this is only for current Jwt token):
User.Identity.GetFirstClaimValueByType("id")
I read other questions and someone suggests to use
string username = HttpContext.Current.GetOwinContext()
.GetUserManager<ApplicationUserManager>().FindById(ID).UserName;
So I try to use HttpContextAccessor but does not have extension method for GetOwinContext
, how can I access it.
Try:
But now I want to send id to get a username is that possible? Regards
_httpContextAccessor.HttpContext
Here is an example of how you can use the ApplicationUserManager
inside of your controller.
public class MyController : ControllerBase
{
private static ApplicationUserManager _userManager;
public MyController(ApplicationUserManager userManager)
{
_userManager = userManager;
}
[HttpGet]
public async Task<IActionResult> Get()
{
var user = await _userManager.GetUserAsync(User);
var userName = user.UserName;
}
}
Please note: The type of ApplicationUserManager
must be the same type of UserManager<TUser>
registered by the Startup.cs for your Identity classes.