AspNet Core 3.1:错误将IdentityUser转换为ApplicationUser



在asp net core mvc 3.1中,我在尝试将IdentityUser转换为ApplicationUser 时出错

我创建了以下类来自定义用户:

public class ApplicationUser: IdentityUser
{
public String NotificationEmail { get; set; }
}

迁移之后,在AspNetUsers表中,我看到NotificationEmail字段。

现在我已经创建了一个控制器/Action

public class UserSettingsController : Controller
{
private readonly UserManager<IdentityUser> _userManager;
public UserSettingsController(UserManager<IdentityUser> userManager)
{
_userManager = userManager;
}
public async Task<IActionResult> Index()
{
var userId = _userManager.GetUserId(User);
ApplicationUser currentUser = await _userManager.FindByIdAsync(userId);
return View(currentUser );
}

问题是编译器在这个指令中:

ApplicationUser currentUser = await _userManager.FindByIdAsync(userId);

告诉我,不可能将类型Microsoft.AspNetCore.Identity.IdentityUser强制转换为ApplicationUser。

如何获取当前ApplicationUser以修改视图中的自定义字段NotificationEmail?

使用UserMnager<ApplicationUser>代替UserManager<IdentityUser>

最新更新