public async Task <AuthenticatedUserModel> AuthenticateAsync(UserAuthenticateCommand command) { var user = await _repository.SingleOrDefaultAsync(x => x.Username.Equals(command.Username)); Guard.Against(user, ErrorType.NotFound); var isCorrectPassword = user.Password.Equals(command.Password); Guard.Against(!isCorrectPassword, ErrorType.IncorrectUserPassword); var tokenExpiration = _appSettings.GetValue <string>("TokenExpiration"); var secret = _appSettings.GetValue <string>("Secret"); var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.ID.ToString()), new Claim(ClaimTypes.Role, user.Role.ToString()), }), Expires = DateTime.UtcNow.AddMinutes(60), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); user.Token = tokenHandler.WriteToken(token); await CommitAsync(); return(_mapper.Map <AuthenticatedUserModel>(user)); }
public async Task <IActionResult> Login([FromBody] UserAuthenticateCommand request) { var result = await _mediator.Send(request); await Request.HttpContext.SignInAsync(new IdentityServerUser(result.UserId) { DisplayName = result.Username }); return(new JsonResult(result)); }
public AuthenticateResult Authenticate(string username, string password) { var command = new UserAuthenticateCommand(); command.Username = username; command.Password = password; return(_mediator.Send(command).Result); }
public async Task <IActionResult> AuthenticateAsync(UserAuthenticateCommand command) { var user = await _userService.AuthenticateAsync(command); return(Ok(user)); }
public IHttpActionResult AuthenticateUser(UserAuthenticateCommand command) { UserAuthenticateCommandResult result = (UserAuthenticateCommandResult)_handlerAuthenticate.Handle(command); return(Response(result)); }
public async Task <IActionResult> Authenticate([FromBody] UserAuthenticateCommand cmd) { return(Ok(await Mediator.Send(cmd))); }
public ICommandResult Handle(UserAuthenticateCommand command) { var commandResult = new UserAuthenticateCommandResult(); if (!(_validationService.Validate(command))) { return(commandResult); } //Validações de coisas que não vão em repositório if (!(command.HasUserName() & command.HasPassword())) { return(commandResult); } //Validações de coisas que vão em repositório var person = _personRepository.GetByEmailAndPassword(command.User, _passwordService.Encrypt(command.Password)); if (!(command.HasFoundAuthorizedUser(person) && command.HasActiveUser(person))) { return(commandResult); } //Trata fluxo demais regras person.SetSerialKey(Guid.NewGuid().ToString().Replace("-", "")); person.SetPushToken(command.PushToken); #region .: Comment :. //var personDevice = _deviceRepository.GetDeviceByPersonIncludingPerson(person); //if (personDevice == null) //{ // var newDevice = new Device(Guid.NewGuid(), description: "", deviceToken: command.Identification, pushToken: "", simCardNumber: "", // deviceOs: DeviceOs.FromValue(command.DeviceOs), identification: command.Identification, person: person); // _deviceRepository.Add(newDevice); //} //else //{ // if (personDevice.Identification != command.Identification) // { // personDevice.Disable(); // personDevice.Deactivate(); // _deviceRepository.Update(personDevice); // var newDevice = new Device(Guid.NewGuid(), description: "", deviceToken: command.Identification, pushToken: "", simCardNumber: "", // deviceOs: DeviceOs.FromValue(command.DeviceOs), identification: command.Identification, person: person); // _deviceRepository.Add(newDevice); // } //} //if (person.Token == null) //{ // var newToken = new Token(tokenId: Guid.NewGuid(), userToken: Guid.NewGuid(), deviceOs: DeviceOs.FromValue(command.DeviceOs)); // _tokenRepository.Add(newToken); // person.SetToken(newToken); //} //else //{ // var token = person.Token; // token.Deactivate(); // _tokenRepository.Update(token); // var newToken = new Token(tokenId: Guid.NewGuid(), userToken: Guid.NewGuid(), deviceOs: DeviceOs.FromValue(command.DeviceOs)); // _tokenRepository.Add(newToken); // person.SetToken(newToken); //} #endregion _personRepository.Update(person); //IEnumerable<CondoPerson> condoPersonList = _condoPersonRepository.GetByPersonIdIncludingCondo(person.PersonId); //var condos = condoPersonList.Select(x => new UserAuthenticateCommandResult.Condo() //{ // CondoId = x.Condo.CondoId.ToString(), // CondoName = x.Condo.Name, // AddressApartment = x.AddressApartment, // AddressBlock = x.AddressBlock, // AddressComplement = x.AddressComplement, // Admin = x.Admin, // Visitor = false, // Resident = x.Resident, // ResidentCode = x.ResidentCode //}).ToList(); //var personVisitorList = _personVisitorRepository.GetByPersonId(person.PersonId); //foreach (var personVisitor in personVisitorList) //{ // if (condos.All(c => c.CondoId != personVisitor.Condo.CondoId.ToString())) // { // condos.Add(new UserAuthenticateCommandResult.Condo() //{ // CondoId = personVisitor.Condo.CondoId.ToString(), // CondoName = personVisitor.Condo.Name, // AddressApartment = string.Empty, // AddressBlock = string.Empty, // AddressComplement = string.Empty, // Admin = false, // Visitor = true, // Resident = false, // ResidentCode = string.Empty // }); //} //else //{ // //condos.FirstOrDefault(c => c.CondoId == personVisitor.Condo.CondoId.ToString()).Visitor = true; // } //} return(new UserAuthenticateCommandResult() { Name = person.Name, SerialKey = person.SerialKey, PushToken = person.PushToken, PhoneNumber = person.PhoneNumber, DocumentNumber = person.DocumentNumber, Email = person.Email //Condos = condos }); }
public UserAuthenticateCommand Build() { var authenticateUserCommand = new UserAuthenticateCommand(_user, _password, _pushToken); return(authenticateUserCommand); }