相关疑难解决方法(0)

获取Asp.Net MVC 5中登录用户的UserID

我是ASP.Net MVC的新手,现在尝试使用内置的用户登录功能.我可以在注册视图中注册用户.如果我尝试使用创建的用户登录,这也有效.我被重定向到母版页.

但是我无法获得当前用户的UserID.我在HomeController和AccountController中尝试了我的代码,但两者都没有用.第一行中的语句返回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());
}
Run Code Online (Sandbox Code Playgroud)

在获取UserID之前,我是否需要做其他事情?

c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity

41
推荐指数
1
解决办法
7万
查看次数