GetUserAsyn returns null [net-core Identity]

0

Hi, I have a net core mvc project, where I use Identity for users, registration, login, etc.

My problem is that when I log in I redirect it to the Index of article, where I want to take the user logged in with await _userManager.GetUserAsync(User) and return null .

I show them part of the code to see if they can help me (if they need to see more tell me). The Login and the Registry within the LoginController, and the Index of the ControlController.

LoginController

public async Task<string> RegisterUser(RegisterViewModel model)
    {
        try
        {
            ErrorViewModel message = new ErrorViewModel();

            bool x = await _roleManager.RoleExistsAsync("Customer");
            if (!x)
            {
                var role = new IdentityRole
                {
                    Name = "Customer"
                };
                await _roleManager.CreateAsync(role);
            }

            User user = new User
            {
                UserName = model.Email,
                Name = model.Name,
                Surname = model.Surname,
                PhoneNumber = model.Phone,
                Email = model.Email,
                Address = model.Address,
                LockoutEnabled = true,
                LockoutEnd = DateTime.Now.AddYears(10)
            };

            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                var result1 = await _userManager.AddToRoleAsync(user, "Customer");
                return "Ok";
            }
            else
            {
                if (result.Errors.First().Code.Equals("PasswordRequiresLower"))
                {
                    message.ErrorMsg = "La clave debe contener al menos una letra.";
                    return "Clave incorrecta";
                }
                message.ErrorMsg = "El email ingresado ya esta registrado en el sistema.";
                return "Email incorrecto";
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return "Error";
        }
    }


public string LogInUser(LoginViewModel model)
    {
        if (_userDataAccess.Exist(model.Username))
        {
            User user = _userManager.FindByEmailAsync(model.Username).GetAwaiter().GetResult();
            if (user == null)
                user = _userManager.FindByNameAsync(model.Username).GetAwaiter().GetResult();

            if (!user.LockoutEnabled && user.AccessFailedCount < 10)
            {
                _userManager.ResetAccessFailedCountAsync(user).GetAwaiter().GetResult();
                _userManager.SetLockoutEnabledAsync(user, false).GetAwaiter().GetResult();
                _userManager.SetLockoutEndDateAsync(user, DateTime.Now.AddDays(365)).GetAwaiter().GetResult();
            }
            else if (user.LockoutEnabled)
            {
                return "Su cuenta esta desactivada.";
            }

            var result = _signInManager.PasswordSignInAsync(user, model.Password, true, lockoutOnFailure: true).GetAwaiter().GetResult();
            if (!_userManager.IsEmailConfirmedAsync(user).GetAwaiter().GetResult())
            {
                if (result.Succeeded)
                {
                    if (user.AccessFailedCount > 0)
                    {
                        _userManager.ResetAccessFailedCountAsync(user).GetAwaiter().GetResult();
                        _userManager.SetLockoutEnabledAsync(user, false).GetAwaiter().GetResult();
                    }
                    return "Ok";
                }
                else if (result.IsLockedOut)
                {
                    return "Su cuenta esta desactivada.";
                }
                else
                {
                    return "Incorrecto";
                }
            }
            else
            {
                return "El usuario no ha confirmado la cuenta";
            }
        }
        else
            return "El nombre de usuario es incorrecto";
    }

ArticleController

public async Task<IActionResult> Index()
    {
        var user = await _userManager.GetUserAsync(HttpContext.User);
        // user queda en null

        List<Articulo> list = _articuloDataAccess.GetAll();
        return View(list);
    }
    
asked by Juan Manuel 02.09.2018 в 05:12
source

1 answer

1

I was missing in startup.cs . before app.useMvc() , add line app.UseAuthentication()

    
answered by 10.09.2018 / 14:06
source