0

(Starred out sensitive details for obvious reasons)

The problem line of code is :

int val = (int)user.Properties["userAccountControl"].Value;

I tried instantiating int val before the try catch, but that doesn't seem to do it. I am guessing that I am incorrectly converting the userAccountControl value to an int that's why it is outputting it as a null value. I have tried moving the (int) function around the line.

I also tried adding parenthesis around the whole thing :

((int)user.Properties["userAccountControl"].Value);

The disable function :

   public void Disable(string userDn)
   {
        try
        {
            DirectoryEntry user = new DirectoryEntry(userDn);
            user.Path = "LDAP://******";
            user.Username = @"********";
            user.Password = "*******";
            int val = (int)user.Properties["userAccountControl"].Value;
            user.Properties["userAccountControl"].Value = val | 0x2;
      
            user.CommitChanges();
            user.Close();
        }
        catch (System.DirectoryServices.DirectoryServicesCOMException E)
        {
            MessageBox.Show(E.Message.ToString());
        }
    }

This is the userDn string and how I run the Disable function:

Disable("CN=Bob Ross,OU=***,DC=***,DC=***");    

Debug error message :

Any help would be much appreciated

Thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sulav.rai
  • 1
  • 1

1 Answers1

0

Without more data, best guess - something is not where it supposed to be or cast failed.

A - string userDn was null and constructor of DirectoryEntry throw
B - constructor did not fail, but something in the (int)user.Properties["userAccountControl"].Value; chain was null.

I would say for start check those 2 options. For 'B', maybe try some checks, like var value = Int.TryParse(user.Properties["userAccountControl"]?.Value , out var result) ? result : throw new... / return -1 / handle error etc

Posted by mobile, sorry for formatting.

quain
  • 861
  • 5
  • 18
  • Thanks for the advice. I figured it out. My userDn path was incorrect that's why it was null when I was trying to set it to my int val. – sulav.rai Sep 10 '21 at 09:28