获取AspNet Core中的当前用户ID

时间:2017-06-19 21:55:47

标签: asp.net-mvc asp.net-web-api asp.net-core asp.net-identity

到目前为止,在Asp.Net中,我使用此命令获取当前用户名

var currentUsername = !string.IsNullOrEmpty(System.Web.HttpContext.Current?.User?.Identity?.Name)
            ? HttpContext.Current.User.Identity.Name
            : "Anonymous";

我应该如何使用此方法在AspnetCore中获取用户名?

谢谢

1 个答案:

答案 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;


}