public async Task <IActionResult> Post([FromForm] AuthenticateTeacherCommand command) { if (command == null) { return(BadRequest(new List <Notification> { new Notification("User", "Usuário ou senha inválidos") })); } var identity = await GetClaims(command); if (identity == null) { return(BadRequest(new List <Notification> { new Notification("User", "Usuário ou senha inválidos") })); } var claims = new[] { new Claim(JwtRegisteredClaimNames.UniqueName, command.Email), new Claim(JwtRegisteredClaimNames.NameId, command.Email), new Claim(JwtRegisteredClaimNames.Email, command.Email), new Claim(JwtRegisteredClaimNames.Sub, command.Email), new Claim(JwtRegisteredClaimNames.Jti, await _tokenOptions.JtiGenerator()), new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_tokenOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64), identity.FindFirst("SoccerManager") }; var jwt = new JwtSecurityToken( issuer: _tokenOptions.Issuer, audience: _tokenOptions.Audience, claims: claims.AsEnumerable(), notBefore: _tokenOptions.NotBefore, expires: _tokenOptions.Expiration, signingCredentials: _tokenOptions.SigningCredentials); var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt); var response = new { token = encodedJwt, expires = (int)_tokenOptions.ValidFor.TotalSeconds, user = new { id = _teacher.Id, name = _teacher.Name.ToString(), email = _teacher.Email, username = _teacher.Email } }; var json = JsonConvert.SerializeObject(response, _serializerSettings); return(new OkObjectResult(json)); }
private Task <ClaimsIdentity> GetClaims(AuthenticateTeacherCommand command) { var teacher = _repository.Get(command.Email); if (teacher == null) { return(Task.FromResult <ClaimsIdentity>(null)); } _teacher = teacher; if (!teacher.Authenticate(command.Email, command.Password)) { return(Task.FromResult <ClaimsIdentity>(null)); } return(Task.FromResult(new ClaimsIdentity( new GenericIdentity(teacher.Email, "Token"), new[] { new Claim("SoccerManager", "Teacher") }))); }