// [Authorize(Roles = "manager")] public ActionResult <SobreCliente> Post( [FromBody] SobreCliente model) { // Verifica se os dados são válidos if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { // Força o usuário a ser sempre "funcionário" model.Role = "employee"; _sobreClienteRepository.Add(model); // Esconde a senha model.Senha = ""; return(model); } catch (Exception) { return(BadRequest(new { message = "Não foi possível criar o usuário" })); } }
public static string GenerateToken(SobreCliente sobreCliente) { var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(Settings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, sobreCliente.NomeApelido.ToString()), new Claim(ClaimTypes.Role, sobreCliente.Role.ToString()) }), Expires = DateTime.UtcNow.AddHours(2), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); return(tokenHandler.WriteToken(token)); }
public SobreCliente Authenticate(SobreCliente sobreCliente) { using (var con = new SqlConnection(_connectionStringGuardBank)) { try { return(con.QueryFirstOrDefault <SobreCliente>( @"SELECT * FROM SobreCliente WHERE Email = @SobreEmail AND Senha = @SobreSenha", new { SobreEmail = sobreCliente.Email, SobreSenha = sobreCliente.Senha })); } catch (Exception ex) { throw ex; } } }
public int Add(SobreCliente sobreCliente) { int count = 0; using (var con = new SqlConnection(_connectionStringGuardBank)) { try { var query = "INSERT INTO SobreCliente(Guid, NomeApelido, Email, ConfirmarEmail, Senha, Role, DataCadastro ) VALUES(@Guid, @NomeApelido, @Email, @ConfirmarEmail, @Senha, @Role, GETDATE() ) "; count = con.Execute(query, sobreCliente); } catch (Exception ex) { throw ex; } return(count); } }
public ActionResult <dynamic> Authenticate( [FromBody] SobreCliente model) { var user = _sobreClienteRepository.Authenticate(model); if (user == null) { return(NotFound(new { message = "Usuário ou senha inválidos" })); } var token = TokenService.GenerateToken(user); // Esconde a senha user.Senha = ""; return(new { user = user, token = token }); }