到目前为止,在Asp.Net中,我使用此命令获取当前用户名
var currentUsername = !string.IsNullOrEmpty(System.Web.HttpContext.Current?.User?.Identity?.Name)
? HttpContext.Current.User.Identity.Name
: "Anonymous";
我应该如何使用此方法在AspnetCore中获取用户名?
谢谢
答案 0 :(得分:1)
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;
}