示例#1
0
        public async Task <LoginResult> LoginAsync(LoginModel loginModel)
        {
            var exists = authDataProvider.Users.FirstOrDefault(x => x.Email == loginModel.Login && x.PasswordHash == cryptService.CreateHash(loginModel.Password));

            if (exists == null)
            {
                return(null);
            }
            var res = await GenerateTokenAndResult(exists);

            await authDataProvider.SaveChangesAsync();

            return(res);
        }
示例#2
0
        public async Task <Response <RegisterUserVM> > Handle(RegisterUserCommand request, CancellationToken cancellationToken)
        {
            var response = new Response <RegisterUserVM>();

            try
            {
                if (!_repository.ObterQueryEntidade().AsNoTracking().Any(u => u.Email == request.Email))
                {
                    var user = _mapper.Map <RegisterUserCommand, User>(request);

                    user.Created = DateTime.Now;

                    user.Username = _accountService.GenerateUsername(user.Name, user.LastName);

                    user.Password = _cryptService.CreateHash(user.Password);

                    var userCriado = await _repository.CriarEntidadeAsync(user);

                    var resultado = _mapper.Map <User, RegisterUserVM>(userCriado);

                    return(await Task.FromResult(new Response <RegisterUserVM>(resultado)));
                }
                else
                {
                    response.AddError(nameof(request.Email), "E-mail já existente.");
                }
            }
            catch (Exception e)
            {
                response.AddError(e.Source, e.Message);
            }

            return(await Task.FromResult(response));
        }
        public async Task <Response <LoginUserVM> > Handle(LoginUserQuery request, CancellationToken cancellationToken)
        {
            var response = new Response <LoginUserVM>();

            try
            {
                string hash = _cryptService.CreateHash(request.Password);

                Expression <Func <User, bool> > validUser = u =>
                                                            u.Email.ToLower() == request.Email.ToLower() &&
                                                            u.Password == hash;

                var user = _repository.ObterQueryEntidade().AsNoTracking().FirstOrDefault(validUser);

                if (user is null)
                {
                    response.AddError("Erro ao efetuar login", "E-mail ou senha inválidos.");

                    return(await Task.FromResult(response));
                }

                var authentication = _authenticationService.CreateTokenJWT(user);

                var viewModel = new LoginUserVM
                {
                    Access_token = authentication.Token,
                    Token_type   = authentication.Type,
                    Expires_in   = authentication.Expires
                };

                return(await Task.FromResult(new Response <LoginUserVM>(viewModel)));
            }
            catch (Exception e)
            {
                response.AddError(e.Source, e.Message);
            }

            return(await Task.FromResult(response));
        }