public async Task <IActionResult> Put(int id, DuenioEvento entidad) { try { //edita solo el propie logeado if (ModelState.IsValid && contexto.DuenioEvento.AsNoTracking().SingleOrDefault(e => e.IdDuenioEvento == id && e.Correo == User.Identity.Name) != null) { entidad.IdDuenioEvento = id; entidad.Clave = Convert.ToBase64String(KeyDerivation.Pbkdf2( password: entidad.Clave, salt: System.Text.Encoding.ASCII.GetBytes("SALADA"), prf: KeyDerivationPrf.HMACSHA1, iterationCount: 1000, numBytesRequested: 256 / 8)); contexto.DuenioEvento.Update(entidad); contexto.SaveChanges(); return(Ok(entidad)); } return(BadRequest()); } catch (Exception ex) { return(BadRequest(ex)); } }
public async Task <IActionResult> Login(DuenioEvento entidad) { try { string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2( password: entidad.Clave, salt: System.Text.Encoding.ASCII.GetBytes(config["Salt"]), prf: KeyDerivationPrf.HMACSHA1, iterationCount: 1000, numBytesRequested: 256 / 8)); var p = contexto.DuenioEvento.FirstOrDefault(x => x.Correo == entidad.Correo); if (p == null || p.Clave != hashed) { return(BadRequest("Nombre de usuario o clave incorrecta")); } else { var key = new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes(config["TokenAuthentication:SecretKey"])); var credenciales = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var claims = new List <Claim> { new Claim(ClaimTypes.Name, p.Correo), new Claim("FullName", p.Nombre + " " + p.Apellido), new Claim(ClaimTypes.Role, "DuenioEvento"), }; var token = new JwtSecurityToken( issuer: config["TokenAuthentication:Issuer"], audience: config["TokenAuthentication:Audience"], claims: claims, expires: DateTime.Now.AddMinutes(60), signingCredentials: credenciales ); return(Ok(new JwtSecurityTokenHandler().WriteToken(token))); } } catch (Exception ex) { return(BadRequest(ex)); } }
public async Task <IActionResult> Post(DuenioEvento entidad) { try { if (ModelState.IsValid) { entidad.Clave = Convert.ToBase64String(KeyDerivation.Pbkdf2( password: entidad.Clave, salt: System.Text.Encoding.ASCII.GetBytes("SALADA"), prf: KeyDerivationPrf.HMACSHA1, iterationCount: 1000, numBytesRequested: 256 / 8)); contexto.DuenioEvento.Add(entidad); contexto.SaveChanges(); return(CreatedAtAction(nameof(Get), new { id = entidad.IdDuenioEvento }, entidad)); } return(BadRequest()); } catch (Exception ex) { return(BadRequest(ex)); } }