我刚刚转而使用新的 2.0 版身份框架。在 1.0 中,我可以使用 manager.FindByIdAsync(User.Identity.GetUserId())
获取用户对象。 GetUserId()
方法似乎在 2.0 中不存在。
现在我能想到的就是使用 manager.FindByEmailAsync(User.Identity.Name)
来引用用户表中的用户名字段。在我的应用程序中,这设置为与电子邮件字段相同。
当有人需要更新他们的电子邮件时,我可以看到这会导致问题。有没有一种方法可以根据身份 2.0 框架中不变的值(例如 id 字段) 获取当前登录的用户对象?
最佳答案
GetUserId()
是 IIdentity
的扩展方法,它在 Microsoft.AspNet.Identity.IdentityExtensions
中。确保您已使用 using Microsoft.AspNet.Identity;
添加命名空间。
关于c# - 在 ASP.NET Identity 2.0 中获取当前用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22624470/