Ejemplo n.º 1
0
        public async Task ResetPasswordAsync(ResetUserPasswordCommand command)
        {
            if (NotifyCommandErrors(command))
            {
                return;
            }

            NutrientIdentityUser user = await _userManager.FindByEmailAsync(command.Email);

            if (NotifyNullUser(user, command.Email))
            {
                return;
            }

            IdentityResult result = await _userManager.ResetPasswordAsync(user, command.ResetToken, command.NewPassword);

            if (!result.Succeeded)
            {
                NotifyIdentityErrors(result);
            }
        }
Ejemplo n.º 2
0
        public async Task <ClaimsIdentity> LoginAsync(LoginUserCommand command)
        {
            if (NotifyCommandErrors(command))
            {
                return(null);
            }

            NutrientIdentityUser user = await _userManager.FindByEmailAsync(command.Email);

            if (NotifyNullUser(user, command.Email))
            {
                return(null);
            }

            SignInResult result = await _signInManager.CheckPasswordSignInAsync(user, command.Password, false);

            if (result.Succeeded)
            {
                IEnumerable <Claim> claims = await _userManager.GetClaimsAsync(user);

                IEnumerable <string> roles = await _userManager.GetRolesAsync(user);

                List <Claim> userPermissions = new List <Claim>();
                userPermissions.AddRange(claims);
                userPermissions.AddRange(roles.Select(value => new Claim(ClaimTypes.Role, value)));

                GenericIdentity genericIdentity = new GenericIdentity(user.Id.ToString());
                ClaimsIdentity  claimsIdentity  = new ClaimsIdentity(genericIdentity, userPermissions);

                _logger.LogInformation("Autenticação bem sucedida para o usuário {email}.", user.Email);

                return(claimsIdentity);
            }

            _logger.LogWarning("Autenticação do usuário {email} falhou.", command.Email);

            AddNotification("Usuário inválido", "O usuário e/ou senha estão incorretos.");
            return(null);
        }
Ejemplo n.º 3
0
        public async Task ConfirmEmailAsync(ConfirmUserEmailCommand command)
        {
            if (NotifyCommandErrors(command))
            {
                return;
            }

            NutrientIdentityUser user = await _userManager.FindByEmailAsync(command.Email);

            if (NotifyNullUser(user, command.Email))
            {
                return;
            }

            IdentityResult result = await _userManager.ConfirmEmailAsync(user, command.ConfirmationToken);

            if (!result.Succeeded)
            {
                NotifyIdentityErrors(result);
            }
            else
            {
                IdentityResult claimSuccess = await _userManager.AddClaimAsync(user, new Claim("ActiveProfile", "true"));

                if (claimSuccess.Succeeded)
                {
                    await PublishAsync(new UserConfirmedEmailIntegrationEvent(user.Id, user.Email));
                }
                else
                {
                    _logger.LogError("Falha ao adicionar claim de confirmação de e-mail para usuário {email}. Motivo: {errors}.", user.Email, claimSuccess.Errors);

                    AddNotification("Falha nas claims", "Ocorreu uma falha ao registrar ou atualizar as claims padrões desse usuário.");
                }
            }
        }
Ejemplo n.º 4
0
        public async Task <bool> CheckUsernameAvailabilityAsync(string username)
        {
            NutrientIdentityUser existingUser = await _userManager.FindByNameAsync(username);

            return(existingUser == null);
        }