public async Task <IActionResult> Login(UserForLoginDto userForLoginDto) { var user = await _repo.Login(userForLoginDto.Username.ToLower(), userForLoginDto.Password); if (user == null) { return(Unauthorized()); } var claims = new[] { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Name, user.Username) }; var key = new SymmetricSecurityKey(Encoding.UTF8 .GetBytes(_config.GetSection("AppSettings:Token").Value)); var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = DateTime.Now.AddDays(1), SigningCredentials = credentials }; var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.CreateToken(tokenDescriptor); var userToStoreInToken = _mapper.Map <UserForListDto>(user); return(Ok(new { token = tokenHandler.WriteToken(token), userToStoreInToken })); }
public CommandResultToken Login(string userName, string password) { var user = _repository.GetSalt(userName); if (user == null) { return(new CommandResultToken(false, "Login inválido. ", null)); } var salt_tabela = user.Salt; byte[] salt = Convert.FromBase64String(salt_tabela); var hashPassword = Hash.Create(password, salt); // <-- monta hash para comparação / login user = _repository.Login(userName, hashPassword); if (user == null) { return(new CommandResultToken(false, "Login inválido. ", null)); } if (user.Active == false && user.Role == null) { AddNotification("Aguardando liberação de acesso. "); } else if (user.Active == false) { AddNotification("Usuário inativo. Contacte o Administrado. "); } if (Invalid) { return(new CommandResultToken(false, GroupNotifications.Group(Notifications), null)); } var log = new AccessLog( "Login", DateTime.Now, userName, null, null); _log.Register(log); user.HidePassword(); return(new CommandResultToken(true, "Login efetuado com sucesso! ", user)); }
private async Task <ClaimsIdentity> CreateIdentity(string email, string password) { var student = await _authRepos.Login(email, password); if (student != null) { List <Claim> claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, student.StudentId.ToString()), new Claim(ClaimTypes.Name, student.FirstName), new Claim(ClaimTypes.Email, student.Email), new Claim(ClaimTypes.Role, student.Role), }; ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims); return(claimsIdentity); } return(null); }
private async Task <ClaimsIdentity> CreateEmployerIdentity(string email, string password) { var employer = await _authRepos.Login(email, password); if (employer != null) { var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, employer.EmployerId.ToString()), new Claim(ClaimTypes.Name, employer.FirstName), new Claim(ClaimTypes.Email, employer.Email), new Claim(ClaimTypes.Role, employer.Role), new Claim(ClaimTypes.UserData, employer.CompanyName), }; ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims); return(claimsIdentity); } return(null); }