Пример #1
0
        public ActionResult Login([FromBody] ACuenta cuenta)
        {
            var       secretKey = Startup.cadenaToken;
            var       key       = Encoding.ASCII.GetBytes(secretKey);
            CuentaImp cuentaImp = new CuentaImp(new CuentaPersistencia());
            APersona  perosna   = cuentaImp.Login(cuenta);

            if (perosna == null)
            {
                return(NotFound());
            }

            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, cuenta.NombreUsuario),
                new Claim(ClaimTypes.Actor, perosna.Rol)
            };
            ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = claimsIdentity,
                Expires            = DateTime.UtcNow.AddHours(8),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            var createdToken = tokenHandler.CreateToken(tokenDescriptor);

            var token = tokenHandler.WriteToken(createdToken);

            return(Ok(new { rol = perosna.Rol, token, idPersona = perosna.IdPersona }));
        }
Пример #2
0
        public ActionResult <APersona> Registrar([FromBody] Cuenta cuenta)
        {
            PersonaImp personaImp = new PersonaImp(new PersonaPersistencia());
            APersona   personaRegistrada;

            if (!personaImp.validarNombreUsuarioRepetido(cuenta.NombreUsuario))
            {
                using (TransactionScope tran = new TransactionScope())
                {
                    personaRegistrada = personaImp.Registar(cuenta.Persona);

                    CuentaImp cuentaImp = new CuentaImp(new CuentaPersistencia());
                    cuenta.Persona.IdPersona = personaRegistrada.IdPersona;
                    cuentaImp.Registar(cuenta);

                    tran.Complete();
                }
            }
            else
            {
                return(BadRequest());
            }
            return(personaRegistrada);
        }