Reputation: 33
So far in Asp.Net I have used this command to get current username
var currentUsername = !string.IsNullOrEmpty(System.Web.HttpContext.Current?.User?.Identity?.Name)
? HttpContext.Current.User.Identity.Name
: "Anonymous";
How should I use this method to get username in AspnetCore ?
Thank you
Upvotes: 0
Views: 969
Reputation: 1214
private readonly UserManager<User> _userManager ;
public LoginController(UserManager<User> userManager){
_userManager=userManager
}
public IActionResult GetUserName()
{
User user = _userManager.GetUserAsync(HttpContext.User).Result;
var userName=user.Username;
}
Upvotes: 1