public async Task RequestUpdateEmail(UpdateUSerEmailAccountDto userParams, string idUser, IUrlHelper url)
        {
            if (userParams == null || idUser == null)
            {
                throw new AppException("The data provided is not correct");
            }

            var user = await _userManager.FindByIdAsync(idUser);

            if (user == null)
            {
                throw new AppException("Utilizador Invalido!");
            }

            //if (userParams.Email.Equals(user.Email, StringComparison.InvariantCultureIgnoreCase))
            //    throw new AppException("O seu novo email e o mesmo da sua actual conta! Por favor indique um email diferente.");

            //var userEntityUsername = await _userManager.FindByEmailAsync(userParams.Email);
            //    if (userEntityUsername != null)
            //        throw new AppException("Email is already assigned to another user!");

            var token = await _userManager.GenerateChangeEmailTokenAsync(user, userParams.Email);

            var resetLink = url.Action("UpdateEmailConfirmationTokenAsync", "Account",
                                       new { token = token, userId = user.Id, newEmail = userParams.Email }, protocol: _httpContext.HttpContext.Request.Scheme);

            Helpers.SendGrid sendEmail = new Helpers.SendGrid(_configuration);

            await sendEmail.PostMessageUpdateEmail(userParams.Email, resetLink);
        }
Example #2
0
        public async Task SendEmailForgetPassword(ForgetPasswordDto forgetPasswordDto, IUrlHelper url)
        {
            if (forgetPasswordDto.Email == null)
            {
                throw new AppException("O Email é de preenchimento obrigatorio!");
            }

            var userEntity = await _userManager.FindByEmailAsync(forgetPasswordDto.Email);

            if (userEntity == null)
            {
                throw new AppException("Email [ " + forgetPasswordDto.Email + " ] dont exist!");
            }

            var token = await _userManager.GeneratePasswordResetTokenAsync(userEntity);

            if (token == null)
            {
                throw new AppException("Erro ao gerar password reset token!");
            }

            var forgetPasswordLink = url.Action("NewPasswordFromForgetPassword", "Auth",
                                                new { token = token, id = userEntity.Id }, protocol: _httpContext.HttpContext.Request.Scheme);

            if (forgetPasswordLink == null)
            {
                throw new AppException("Erro ao gerar link para confirmacao de email!");
            }

            Helpers.SendGrid sendEmail = new Helpers.SendGrid(_configuration);
            var resulsendEmail         = await sendEmail.PostMessageForgetPassword(userEntity.Email, forgetPasswordLink);

            if (resulsendEmail.StatusCode != System.Net.HttpStatusCode.Accepted)
            {
                throw new AppException("Foi impossivel enviar o email para o reset a password! Por favor contactar um administrador");
            }
        }
Example #3
0
        public async Task SendEmailFromConfirmation(string email, IUrlHelper url)
        {
            if (email == null)
            {
                throw new AppException("Os dados para confirmar o email nao estao correctos!");
            }

            var userEntity = await _userManager.FindByEmailAsync(email);

            if (userEntity == null)
            {
                throw new AppException("Email [ " + email + " ] dont exist!");
            }

            var token = await _userManager.GenerateEmailConfirmationTokenAsync(userEntity);

            if (token == null)
            {
                throw new AppException("Erro ao gerar token para envio de email!");
            }

            var emailConfirmationLink = url.Action("EmailConfirmationAfterRegistration", "Auth",
                                                   new { token = token, email = email }, protocol: _httpContext.HttpContext.Request.Scheme);

            if (emailConfirmationLink == null)
            {
                throw new AppException("Erro ao gerar link para confirmacao de email!");
            }

            Helpers.SendGrid sendEmail = new Helpers.SendGrid(_configuration);
            var resulsendEmail         = await sendEmail.PostMessageConfirmRegister(email, emailConfirmationLink);

            if (resulsendEmail.StatusCode != System.Net.HttpStatusCode.Accepted)
            {
                throw new AppException("Foi impossivel enviar o email de confirmação! Por favor contactar um administrador");
            }
        }