public async Task <IActionResult> Login([FromBody] LoginDto login) { try { var isAuthenticated = await _userService.Authenticate(login.Username, login.Password); if (!isAuthenticated) { return(Unauthorized(new { message = "Invalid Credentials" })); } var tokenDto = new CreateTokenDto { Issuer = _config["Jwt:Issuer"], Key = _config["Jwt:Key"] }; var token = _tokenService.CreateToken(tokenDto); return(Json(token)); } catch (Exception) { return(BadRequest()); } }
public async Task CreateTokenWithNotExistedUserTest() { var dto = new CreateTokenDto { Payload = "a", SourceId = "a", UserId = "12" }; Assert.ThrowsAsync <NotFoundException>(async() => await _tokenService.CreateToken(dto)); }
public string CreateToken(CreateTokenDto createToken) { var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(createToken.Key)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken(createToken.Issuer, createToken.Issuer, expires: DateTime.Now.AddMinutes(30), signingCredentials: creds); return(new JwtSecurityTokenHandler().WriteToken(token)); }
public JwtTokenDto GenerateToken(CreateTokenDto value) { Logger.LogInformation($"Try to generate token for user {value.UserName}"); Validate(value); var utcNow = DateTimeOffset.UtcNow; var claims = new List <Claim>(); claims.Add(new Claim(ClaimName.ClientId, value.ClientPublicId)); claims.Add(new Claim(ClaimName.TokenName, value.TokenName)); claims.Add(new Claim(ClaimName.Issued, utcNow.ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture))); claims.Add(new Claim(ClaimName.Name, !String.IsNullOrEmpty(value.UserName) ? value.UserName : String.Empty)); claims.Add(new Claim(ClaimName.Scope, !String.IsNullOrEmpty(value.Scope) ? value.Scope : String.Empty)); var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.SecurityKey)); var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken( issuer: Configuration.Issuer, audience: Configuration.Audience, claims: claims, signingCredentials: credentials, expires: utcNow.AddSeconds(value.SecondsLifeTime).UtcDateTime); var toReturn = new JwtTokenDto() { ClientId = value.ClientPublicId, Expire = utcNow.AddSeconds(value.SecondsLifeTime).ToUnixTimeSeconds(), IsValid = true, Scope = value.Scope, Token = new JwtSecurityTokenHandler().WriteToken(token), UserName = value.UserName }; return(toReturn); }
public async Task <Contract.Models.Token> CreateToken(CreateTokenDto createTokenDto) { ValidationHelper.ValidateAndThrow(createTokenDto); if (!await _userGetOperations.ExistsById(createTokenDto.UserId)) { throw new NotFoundException("Пользователь не найден"); } if (!await _sourceGetOperations.ExistsById(createTokenDto.SourceId)) { throw new NotFoundException("Источник не найден"); } var model = new Contract.Models.Token { Id = Guid.NewGuid().ToString(), Payload = createTokenDto.Payload, SourceId = createTokenDto.SourceId, UserId = createTokenDto.UserId }; return(await _tokenWriteOperations.Create(model)); }
public JwtTokenDto GenerateToken(CreateTokenDto value) { _token.Token = GenerateRandomString(20); return(_token); }
public async Task CreateInvalidTokenTest() { var dto = new CreateTokenDto(); Assert.ThrowsAsync <ValidationException>(async() => await _tokenService.CreateToken(dto)); }