예제 #1
0
        public static LOG_IN Validate(LOG_IN cuenta)
        {
            DeliveryEntidades1 db = new DeliveryEntidades1();

            return(db.LOG_IN.FirstOrDefault(x => x.usuario == cuenta.usuario &&
                                            x.contrasena == cuenta.contrasena));

            /*            foreach (var item in db.Usuarios.ToList())
             *          {
             *              if (item.correo == persona.correo && item.password == persona.password)
             *              {
             *                  return item;
             *              }
             *          }
             *          return null;*/
        }
예제 #2
0
        public IHttpActionResult Authenticate(LOG_IN usuario)
        {
            if (usuario == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            usuario = LogInBLL.Validate(usuario);
            if (usuario != null)
            {
                return(Ok(new
                {
                    user = usuario,
                    token = TokenGenerator.GenerateTokenJwt(usuario)
                }));
            }
            else
            {
                return(Unauthorized());
            }
        }
예제 #3
0
        public static string GenerateTokenJwt(LOG_IN persona)
        {
            // RECUPERAMOS LAS VARIABLES DE CONFIGURACIÓN
            var secretKey     = ConfigurationManager.AppSettings["JWT_SECRET_KEY"];
            var audienceToken = ConfigurationManager.AppSettings["JWT_AUDIENCE_TOKEN"];
            var issuerToken   = ConfigurationManager.AppSettings["JWT_ISSUER_TOKEN"];
            var expireTime    = ConfigurationManager.AppSettings["JWT_EXPIRE_MINUTES"];

            // CREAMOS EL HEADER //
            var securityKey        = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
            var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
            var _Header            = new JwtHeader(signingCredentials);
            // CREAMOS LOS CLAIMS //
            var _Claims = new[] {
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim(JwtRegisteredClaimNames.NameId, persona.idlogin.ToString()),
                new Claim("rol", persona.rol),
                new Claim("Nombre", persona.usuario),
                new Claim(JwtRegisteredClaimNames.UniqueName, persona.usuario),
                new Claim(ClaimTypes.Role, persona.rol)
            };
            // CREAMOS EL PAYLOAD //
            var _Payload = new JwtPayload(
                issuer: issuerToken,
                audience: audienceToken,
                claims: _Claims,
                notBefore: DateTime.UtcNow,
                // Expira en 10 min.
                expires: DateTime.UtcNow.AddMinutes(Convert.ToInt32(expireTime))
                );

            // GENERAMOS EL TOKEN //
            var _Token = new JwtSecurityToken(
                _Header,
                _Payload
                );

            return(new JwtSecurityTokenHandler().WriteToken(_Token));
        }