Using MVC 5's identity, can't get user name right after logging in

我的未来我决定 提交于 2019-12-01 16:23:30

At the end of its execution chain SignInManager.PasswordSignInAsync method calls for SignInAsync method which is basically responsible for setting an authentication cookie which contains multiple claims about a user (one of them is it's name). The reason why you can't use User.Identity.Name in the same call with SignInManager.PasswordSignInAsync is that User.Identity filled out with the claims from authentication cookie (which are not parsed yet). The reason that it works on the second pass is that the cookie is there and User.Identity is properly filled. Most of the time after a successful login there is a redirect to some page. Inside this redirect action you will be able to use User.Identity since the cookie is already set.

I've found you can go right back to the UserManager and look them up:

var user = await UserManager.FindAsync(username, password);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!