예제 #1
0
        //CREATE NEW USER
        public User CreateUser(string username, string password, string surname, string lastname, int age, string email)
        {
            Hashing.HashSalt hashSalt = hashing.PasswordHash(16, password);

            using var ctx = new ImdbContext();
            var maxId = ctx.users.Max(x => x.Id);

            /*if (!Regex.IsMatch(username, @"^[a-zA-Z]+$") || !Regex.IsMatch(surname, @"^[a-zA-Z]+$") || !Regex.IsMatch(lastname, @"^[a-zA-Z]+$") || !IsValidEmail(email) ||
             *  age == 0) return null;*/// TODO: Add username & password check
            ctx.users.Add(new User
            {
                Id = maxId + 1, Username = username, Password = hashSalt.Hash, Salt = hashSalt.Salt, Age = age, Surname = surname, Last_Name = lastname, Email = email
            });
            ctx.SaveChanges();
            return(ctx.users.Find(maxId + 1));
        }
예제 #2
0
        //CHANGE PASSWORD
        public bool ChangePassword(/*int id,*/ string username, string oldpassword, string newpassword)
        {
            using var ctx = new ImdbContext();
            var getUser = ctx.users.FirstOrDefault(x => x.Username == username);

            if (_userValidation.VerifyPassword(oldpassword, getUser.Password, getUser.Salt))

            /*
             * if (_userValidation.VerifyPassword(oldpassword, ctx.users.Find(id).Password, ctx.users.Find(id).Salt)
             *  && ctx.users.Find(id).Username == username)*/
            {
                Hashing.HashSalt hashSalt = hashing.PasswordHash(16, newpassword);
                //ctx.users.Update(ctx.users.Find(id)).Entity.Password = hashSalt.Hash;
                //ctx.users.Update(ctx.users.Find(id)).Entity.Salt = hashSalt.Salt;
                ctx.users.Update(getUser).Entity.Password = hashSalt.Hash;
                ctx.users.Update(getUser).Entity.Salt     = hashSalt.Salt;
                ctx.SaveChanges();
            }
            else
            {
                return(false);
            }
            return(true);
        }