Tom
Tom

Reputation: 4107

AspNet Core 3.1: error cast IdentityUser to ApplicationUser

In asp net core mvc 3.1 i get an error when try to cast IdentityUser to ApplicationUser

I have created the following class to customize the user:

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

After the migration, in the table AspNetUsers i see the NotificationEmail Field.

Now i have create a controller/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 );
   }

The problem is that the compiler, in this instruction:

ApplicationUser currentUser = await _userManager.FindByIdAsync(userId);

tell me that it's impossible to cast the type Microsoft.AspNetCore.Identity.IdentityUser to ApplicationUser.

How can i get the current ApplicationUser in order to modify the custom field NotificationEmail in the view?

Upvotes: 1

Views: 671

Answers (1)

Mohamed Adel
Mohamed Adel

Reputation: 490

use UserMnager<ApplicationUser> Insted of UserManager<IdentityUser>

Upvotes: 2

Related Questions