public JwtToken CreateTokenWithLogin(T login, string Role) { JwtToken jwtToken = new JwtToken(); try { var mqResponse = broker.SendBroker <T>(login); if (mqResponse.Code != Enums.GetEnumDescription(ResponseCode.Success)) { jwtToken.Code = mqResponse.Code; jwtToken.Messages = mqResponse.Messages; return(jwtToken); } jwtToken.isFirstLogin = mqResponse.isFirstLogin.ToString(); GenerateRefreshAndUpdateUser(login, (_rt) => { if (string.IsNullOrEmpty(_rt)) { throw new Exception("Refresh Token no pudo ser generado"); } jwtToken.RefreshToken = _rt; }); jwtToken.Token = tokenService.GenerateToken(new Claim[] { new Claim(ClaimTypes.Role, Role), new Claim("UserName", login.UserName), new Claim("DocumentType", !string.IsNullOrEmpty(login.DocumentType) ? login.DocumentType : string.Empty) }); } catch (MQException ex) { throw ex; } catch (Exception ex) { throw ex; } return(jwtToken); }
public async Task <IActionResult> Register(UserForRegisterDto userForRegisterDto) { userForRegisterDto.Username = userForRegisterDto.Username.ToLower(); if (await _repo.UserExists(userForRegisterDto.Username)) { return(BadRequest("Username already exists")); } var userToCreate = _mapper.Map <User>(userForRegisterDto); userToCreate.Token = _token.GenerateToken(128); var createdUser = await _repo.Register(userToCreate, userForRegisterDto.Password); try { var verifyLink = string.Format("{0}://{1}{2}/verify?token={3}", Request.Scheme, Request.Host, Request.Path.Value.Remove(Request.Path.Value.LastIndexOf('/')), HttpUtility.UrlEncode(createdUser.Token)); await _mailer.SendVerificationMail( new MailUser { Email = createdUser.Email, Name = createdUser.Name }, verifyLink ); } catch (Exception ex) { _logger.LogError(ex, "Error sending mail"); return(BadRequest("User created successfully, but verify email failed to send")); } var userToReturn = _mapper.Map <UserForDetailedDto>(createdUser); return(CreatedAtRoute("GetUser", new { controller = "Users", id = createdUser.Id }, userToReturn)); }
public ActionResult <ClientToken> Login([FromBody] Client client) { if (ModelState.IsValid) { if (Secrets.appKey == client.appKey && Secrets.appSecret == client.appSecret) { return new ClientToken() { Token = tokenGenerator.GenerateToken(client.appKey), ClientName = client.appKey } } ; } return(BadRequest(ModelState)); } }
public async Task <CommandReturn> GravarLoginAsync(User user) { user.LastLoginDate = DateTime.Now; user.UpdateDate = user.LastLoginDate; user.Token = _token.GenerateToken(user); if (user.Token.IsEmpty()) { return(new CommandReturn(Messages.TokenNotFound)); } var validationResult = await _validator.ValidateAsync(user); if (!validationResult.IsValid) { return(new CommandReturn(validationResult.Errors)); } return(await _repository.UpdateAsync(user)); }
public void GenerateToken_ReturnsToken() { var result = _hasher.GenerateToken(); Assert.GreaterOrEqual(result.Length, 50); }