예제 #1
0
        public async Task CreateAdministradorAsync(IServiceProvider serviceProvider)
        {
            using var contexto    = serviceProvider.GetRequiredService <LkContextDB>();
            using var userManager = serviceProvider.GetRequiredService <UserManager <Cliente> >();
            var userexists = await userManager.FindByNameAsync("*****@*****.**");

            if (userexists == null)
            {
                var cpf = new CPF("886.922.770-70");
                cpf.SemFormatacao();

                var usuario = new Cliente()
                {
                    UserName = cpf.Codigo,
                    Email    = "*****@*****.**"
                };

                var resultRegisterAccount = await userManager.CreateAsync(usuario, "Senh@0102");

                if (resultRegisterAccount.Succeeded)
                {
                    await userManager.AddToRoleAsync(usuario, "ADMINISTRADOR");
                }
            }
        }
예제 #2
0
        public async Task <IActionResult> CriarEndereco(EnderecoViewModel model, UsuarioViewModel usuariomodel)
        {
            var cpf = new CPF(usuariomodel.Cpf);

            cpf.SemFormatacao();

            var usuario = await _userManager.FindByNameAsync(cpf.Codigo);

            if (usuario != null)
            {
                var endereco = new Endereco()
                {
                    ClienteId   = usuario.Id,
                    Cep         = model.Cep,
                    Logradouro  = model.Logradouro,
                    Numero      = model.Numero,
                    Uf          = model.Uf,
                    Bairro      = model.Bairro,
                    Cidade      = model.Cidade,
                    Complemento = model.Complemento
                };

                usuario.Endereco = endereco;
                var result = await _userManager.UpdateAsync(usuario);

                if (!result.Succeeded)
                {
                    _toastNotification.AddAlertToastMessage("Endereço não foi cadastrado!");
                    return(View(model));
                }
            }

            _toastNotification.AddSuccessToastMessage("Endereco Cadastrado!");
            return(RedirectToAction("Index", "Home"));
        }
예제 #3
0
        public async Task <IActionResult> Editar(EditarUsuarioViewModel model)
        {
            if (string.IsNullOrEmpty(model.Cpf))
            {
                _toastNotification.AddErrorToastMessage("CPf Invalido!");
                return(View(model));
            }
            var cpf = new CPF(model.Cpf);

            cpf.SemFormatacao();

            var usuario = await _userManager.FindByNameAsync(cpf.Codigo);

            if (usuario != null)
            {
                usuario.UserName     = cpf.Codigo;
                usuario.NomeCompleto = model.NomeCompleto;
                usuario.Email        = model.Email;
                usuario.PhoneNumber  = model.Telefone;

                await _userManager.UpdateAsync(usuario);

                _toastNotification.AddSuccessToastMessage("Alteração Salva!");
                return(RedirectToAction("ListaDeUsuarios", new { cpf = model.Cpf }));
            }
            _toastNotification.AddErrorToastMessage("Usuario não encotrado!");
            return(View(model));
        }
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                var login = CPF.TirarFormatacao(model.Login);

                if (CPF.Validar(login) == false)
                {
                    _toastNotification.AddErrorToastMessage("Cpf inválido!");
                    return(View(model));
                }

                var cpf = new CPF(model.Login);
                cpf.SemFormatacao();
                var usuario = await _userManager.FindByNameAsync(cpf.Codigo);

                if (usuario == null)
                {
                    var mensagemUsuario = "Credenciais Inválidas!";
                    _toastNotification.AddErrorToastMessage(mensagemUsuario);
                    ModelState.AddModelError(string.Empty, mensagemUsuario);
                    return(View(model));
                }

                var result = await _signInManager.PasswordSignInAsync
                             (
                    userName : cpf.Codigo,
                    password : model.Senha,
                    isPersistent : model.Lembrar,
                    lockoutOnFailure : true
                             );

                if (result.Succeeded)
                {
                    _toastNotification.AddSuccessToastMessage("Bem Vindo de volta!");
                    _logger.LogWarning($"Logando Usuario{usuario.UserName}, Email: {usuario.Email}.");
                    return(RedirectToAction("Index", "Home"));
                }

                if (result.IsLockedOut)
                {
                    var senhaCorreta = await _userManager.CheckPasswordAsync(usuario, model.Senha);

                    if (senhaCorreta)
                    {
                        var mensagemUsuarioBloqueado = "Conta esta bloqueada!";
                        _logger.LogWarning($"Conta bloqueada Usuario: {usuario.UserName}, Mensagem: {mensagemUsuarioBloqueado}.");
                        _toastNotification.AddErrorToastMessage(mensagemUsuarioBloqueado);
                        ModelState.AddModelError(string.Empty, mensagemUsuarioBloqueado);
                    }
                }

                var mensagemErrorUsuario = "Credenciais inválidas!";
                _toastNotification.AddErrorToastMessage(mensagemErrorUsuario);
                ModelState.AddModelError(string.Empty, mensagemErrorUsuario);
            }
            return(View(model));
        }
        public async Task <IActionResult> AlterarSenha(string cpf)
        {
            var formatoCpf = new CPF(cpf);

            cpf = formatoCpf.Codigo;
            formatoCpf.SemFormatacao();

            var usuario = await _userManager.FindByNameAsync(cpf);

            var token = await _userManager.GeneratePasswordResetTokenAsync(usuario);

            var alterarSenha = new AlteracaoSenhaViewModel
            {
                Cpf   = usuario.UserName,
                Token = token
            };

            return(View(alterarSenha));
        }
예제 #6
0
        public async Task <IActionResult> CriarUsuario(UsuarioViewModel model)
        {
            var cpf = new CPF(model.Cpf);

            cpf.SemFormatacao();

            var userExist = await _userManager.FindByNameAsync(model.Cpf);

            if (userExist != null)
            {
                _toastNotification.AddErrorToastMessage("Usuário já cadastrado!");
                return(View(model));
            }

            var Senha = model.Senha;

            if (Senha != model.ConfirmarSenha)
            {
                _toastNotification.AddErrorToastMessage("Senhas não conferem!");
            }
            else
            {
                var usuario = new Cliente()
                {
                    UserName     = cpf.Codigo,
                    NomeCompleto = model.NomeCompleto,
                    Email        = model.Email,
                    PhoneNumber  = model.Telefone
                };
                await _userManager.CreateAsync(usuario, model.Senha);

                _logger.LogWarning($"Usuatrio criado com sucesso: Usuario{usuario.UserName}, E-mail {usuario.Email}.");
                _toastNotification.AddSuccessToastMessage("Usuário Criado");
                return(RedirectToAction("CriarEndereco", "Endereco", new { cpf = model.Cpf }));
            }
            return(View());
        }