我对ASP比较陌生.Net MVC并try 使用内置的用户登录功能.我可以在注册视图中注册用户.如果我try 使用创建的用户登录,这同样有效.我被重定向到母版页.
但我无法获取当前用户的用户ID.我在HomeController和AccountController中try 了我的代码,但都不起作用.第一行中的语句始终返回null.
var userID = User.Identity.GetUserId();
if (!string.IsNullOrEmpty(userID))
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ApplicationDbContext.Create()));
var currentUser = manager.FindById(User.Identity.GetUserId());
}
在获取用户ID之前,我需要做其他事情吗?