Exemple #1
0
        public void TestaConsultaClientePorNomeUsuario(string userName, string password, string role)
        {
            var user       = new User(userName, password, role);
            var userResult = _tokenHandler.GenerateToken(user).Result;

            Assert.NotNull(userResult);
        }
Exemple #2
0
        public async Task <CustomeSignInResult> LogInUserAsync(Employer user, string password)
        {
            if (_employerHandler.IsAccessable(user))
            {
                var result = await _signInManager.PasswordSignInAsync(user, password, true, false);

                if (result.Succeeded)
                {
                    var token = _tokenHandler.GenerateToken(user);
                    _httpContextAccessor.HttpContext.Response.Cookies.Append(".AspUser",
                                                                             _dataProtectionProvider.CreateProtector(_configuration["dataprotector"]).Protect(token),
                                                                             new CookieOptions()
                    {
                        Expires = DateTime.Now.AddDays(.6)
                    });
                    return(new CustomeSignInResult()
                    {
                        IsLockedOut = result.IsLockedOut,
                        Succeeded = result.Succeeded,
                        IsNotAllowed = result.IsNotAllowed,
                        RequiresTwoFactor = result.RequiresTwoFactor,
                        Errors = (await _employerHandler.LastcheckInAsync(user)).Errors.ToList(),
                        Token = token
                    });
                }
            }
            return(new CustomeSignInResult()
            {
                Errors = new List <IdentityError>()
                {
                    new IdentityError()
                    {
                        Code = "400",
                        Description = "Contact to the shopmanger"
                    },
                    new IdentityError()
                    {
                        Code = "304",
                        Description = "Contact to the inernet"
                    }
                },
                IsLockedOut = false,
                RequiresTwoFactor = false,
                IsNotAllowed = false,
                Succeeded = false,
                Token = null
            });
        }
Exemple #3
0
        public ObjectResult Authorize([FromBody] UserAuthModel userModel)
        {
            if (!ModelState.IsValid)
            {
                _context.Journals.Add(JournalEntryBuilder.CreateEntry("Authorize", false, "Unauthorized"));
                _context.SaveChanges();

                return(new BadRequestObjectResult(ModelState));
            }

            var user = _context.Users.FirstOrDefault(c => c.Email == userModel.Email && c.Password == userModel.Password);

            if (user == null)
            {
                _context.Journals.Add(JournalEntryBuilder.CreateEntry("Authorize", false, "Unauthorized"));
                _context.SaveChanges();

                return(new BadRequestObjectResult("Invalid credentials"));
            }

            List <Claim> claims = new List <Claim>();

            claims.Add(new Claim("user", user.Id.ToString()));
            claims.Add(new Claim("role", user.Status.ToString()));
            var accessToken = _jwtHandler.GenerateToken(claims);
            var tokenString = $"{accessToken.EncodedHeader}.{accessToken.EncodedPayload}.{accessToken.RawSignature}";

            user.Modified = DateTime.Now;
            _context.Update(user);
            _context.Journals.Add(JournalEntryBuilder.CreateEntry("Authorize", true, user.Id.ToString()));

            _context.SaveChanges();

            return(new OkObjectResult(tokenString));
        }
        public async Task <ActionResult <UserDTO> > Login(Login login)
        {
            var user = await _context.Users.Where(x => x.Email == login.Email).AsNoTracking().FirstOrDefaultAsync();

            if (user == null)
            {
                return(new BadRequestResult());
            }

            if (!user.IsApproved)
            {
                return(new BadRequestResult());
            }

            if (!user.Hash.SequenceEqual(_passwordHasher.Hash(user.Salt, login.Password)))
            {
                return(new BadRequestResult());
            }

            var token = await _tokenHandler.GenerateToken(user.Role, user.Email, user.Id.ToString());

            var userdto = new UserDTO(user, token);

            return(new OkObjectResult(userdto));
        }
Exemple #5
0
        public async Task <AuthToken> GetToken(LoginModel loginModel)
        {
            var user = await _userManager.FindByNameAsync(loginModel.UserName);

            if (user == null || !user.EmailConfirmed)
            {
                throw new UserException("Invalid username or password");
            }

            var passwordCheckResult = await _userManager.CheckPasswordAsync(user, loginModel.Password);

            if (!passwordCheckResult)
            {
                throw new UserException("Invalid username or password");
            }

            var token = _tokenHandler.GenerateToken(user);

            return(token);
        }
Exemple #6
0
        public String Authenticate(string email, string password)
        {
            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
            {
                return(null);
            }

            var user = _userDataAccess.GetUserByEmail(email);

            if (user == null)
            {
                return(null);
            }

            if (!VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt))
            {
                return(null);
            }

            return(_tokenHandler.GenerateToken(user.Id));
        }
        public async Task <QueryLoginResult> LoginUser(QueryUserFilter queryUserFilter)
        {
            var user = await _userService.GetUser(queryUserFilter);

            if (user == null || !user.SenhaEValida(queryUserFilter.Password))
            {
                return new QueryLoginResult
                       {
                           Token   = string.Empty,
                           Message = "Usuário não encontrado ou senha inválida",
                           Valid   = false
                       }
            }
            ;
            var token = await _tokenService.GenerateToken(user);

            return(new QueryLoginResult
            {
                Token = token,
                Message = "Token gerado com sucessso",
                Valid = true
            });
        }
    }