Exemple #1
0
        AutenticarRespuestaVModel GenerarToken(UsuariosEModel user)
        {
            var      key = Encoding.ASCII.GetBytes(_configuration["SecretKey"]);
            DateTime TimeSesionOnMinutes = DateTime.UtcNow.AddMinutes(int.Parse(_configuration["TimeSesionOnMinutes"]));


            ClaimsIdentity claims = new ClaimsIdentity();

            claims.AddClaim(new Claim(ClaimTypes.Name, user.Email));
            claims.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
            claims.AddClaim(new Claim(ClaimTypes.Email, user.Email));



            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = claims,
                Expires            = TimeSesionOnMinutes,
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };

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



            return(new AutenticarRespuestaVModel
            {
                Email = user.Email,
                Token = tokenHandler.WriteToken(createdToken)
            });
        }
Exemple #2
0
        public AutenticarRespuestaVModel Autenticar(AutenticarVModel model)
        {
            UsuariosEModel UserEntity = _unitOfWork.Repositories.UsuariosRepository.GetByEmail(model.Email);

            if (UserEntity == null || UserEntity.Contraseina != model.Password)
            {
                return(null);
            }

            return(GenerarToken(UserEntity));
        }
Exemple #3
0
        public UsuariosEModel UserLogued()
        {
            string userEmail = string.Empty;

            using (IServiceScope scope = serviceProvider.CreateScope())
                userEmail = scope.ServiceProvider
                            .GetRequiredService <IHttpContextAccessor>()
                            .HttpContext.User?.Identity?.Name;


            if (userLogued == null)
            {
                userLogued = _unitOfWork.Repositories.UsuariosRepository.GetByEmail(userEmail);
            }

            return(userLogued);
        }