Identity User Data Not Updating

Viewed 188

My custom identity user is not updating but it's not giving any errors either. I'm pasting the whole controller code because I think it's important. Because I'm using AspNetCore.Identity, I can't use AspNet.Identity. When I'm trying to do the Edit action in another controller, I use AspNet.Identity and write something like:

ApplicationUser appUser = new ApplicationUser();
appUser = UserManager.FindByEmail(email);

UserManager gives error https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0305?f1url=%3FappId%3Droslyn%26k%3Dk(CS0305)

I find it worth mentioning that I manually set FirstName and Surname in database and after submitting the button it has changed to NULL.

Account Controller code

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using MySocialService.Data;
using MySocialService.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MySocialService.Controllers
{
    public class AccountController : Controller
    {
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly SignInManager<ApplicationUser> _singInManager;

        public AccountController(UserManager<ApplicationUser> userManager,
                                       SignInManager<ApplicationUser> signInManager)
        {
            _userManager = userManager;
            _singInManager = signInManager;
        }

        public IActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Register(UserRegistrationModel model)
        {
            if(ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName = model.Email,
                    Email = model.Email,
                    FirstName = model.FirstName,
                    Surname = model.Surname,
                    Gender = model.Gender
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if(result.Succeeded)
                {
                    await _singInManager.SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }

                foreach(var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }

                ModelState.AddModelError(string.Empty, "Invalid Login Attempt");
            }

            return View(model);
        }

        [HttpGet]
        [AllowAnonymous]
        public IActionResult Login()
        {
            return View();
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> Login(UserLoginModel user)
        {
            if (ModelState.IsValid)
            {
                var result = await _singInManager.PasswordSignInAsync(user.Email, user.Password,user.RememberMe,false);

                if (result.Succeeded)
                {
                    return RedirectToAction("Dashboard", "MainPage");
                }

                ModelState.AddModelError(string.Empty, "Invalid Login Attempt");

            }
            return View(user);
        }

        public async Task<IActionResult> Logout()
        {
            await _singInManager.SignOutAsync();

            return RedirectToAction("Index", "Home");
        }

        [HttpGet]
        public async Task<IActionResult> Edit(string email)
        {
            ApplicationUser appUser = new ApplicationUser();
            appUser = await _userManager.FindByEmailAsync(email);
            UserRegistrationModel model = new UserRegistrationModel();
            model.FirstName = appUser.FirstName;
            model.Surname = appUser.Surname;
            return View(model);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(UserRegistrationModel model)
        {
            var currentUser = await _userManager.FindByEmailAsync(model.Email);
            model.FirstName = currentUser.FirstName;
            model.Surname = currentUser.Surname;

            await _userManager.UpdateAsync(currentUser);

            return RedirectToAction("Dashboard", "MainPage");
        }
    }
}
1 Answers

I found the answer by debugging. In comment section @Adleri asked what object _userManager.FindByEmailAsync(email) returns so I checked it. It returns ApplicationUser so I changed

var currentUser = await _userManager.FindByEmailAsync(model.Email);

to

ApplicationUser currentUser = new ApplicationUser();
currentUser = await _userManager.FindByEmailAsync(model.Email);

Then I find out that currentUser fields are null so i changed

 model.FirstName = currentUser.FirstName;
 model.Surname = currentUser.Surname;

to

currentUser.FirstName = model.FirstName;
currentUser.Surname = model.Surname;

Simply swap places and it works now.

Related