Esempio n. 1
0
        public UserResponse Auth(AuthRequest authRequest)
        {
            UserResponse userResponse = new UserResponse();

            string passwordEncrypt = Encryp.GetSHA256(authRequest.Password);

            var user = _appDbContext.Users.
                       Where(u => u.Email == authRequest.Email && u.Password == passwordEncrypt)
                       .Include(r => r.Rol)
                       .FirstOrDefault();

            int validezToken = (_appSettings != null ? _appSettings.HorasValidezToken : 1);

            if (user == null)
            {
                return(null);
            }
            userResponse.Email      = user.Email;
            userResponse.Token      = GetToken(user, validezToken);
            userResponse.UserName   = user.UserName;
            userResponse.ExpireDate = DateTime.Now.AddHours(validezToken);
            userResponse.IsAdmin    = (user.Rol.Name == "Admin" ? true : false);

            return(userResponse);
        }
Esempio n. 2
0
        public void Create(User newUser)
        {
            if (newUser == null)
            {
                throw new ArgumentNullException(nameof(newUser));
            }

            newUser.Password = Encryp.GetSHA256(newUser.Password);

            _appDbContext.Users.Add(newUser);
        }
Esempio n. 3
0
        public void Register(User newUser)
        {
            string customerUser = (_appSettings != null ? _appSettings.UserApp : "User");

            if (newUser == null)
            {
                throw new ArgumentNullException(nameof(newUser));
            }

            if (!newUser.RolId.HasValue)
            {
                newUser.RolId = _appDbContext.Roles.FirstOrDefault(r => r.Name == customerUser).Id;
            }

            if (UserExists(newUser))
            {
                throw new Exception("Ya existe un usuario registrado con este correo.");
            }

            newUser.Password = Encryp.GetSHA256(newUser.Password);
            _appDbContext.Users.Add(newUser);
        }