Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 553
  • Last Modified:

Disable/enable user, ADSI

I have this code to enable/disable a user in ADSI:

      const int UF_ACCOUNTDISABLE = 0x0002;
                  
      try
      {
            DirectoryEntry myDirectoryEntry;
            myDirectoryEntry = new DirectoryEntry("WinNT://" + domain +"/"+username+",User");

            myDirectoryEntry.Properties["UserFlags"].Value = ((int)myDirectoryEntry.Properties["UserFlags"].Value) ^ UF_ACCOUNTDISABLE;
                  
            myDirectoryEntry.CommitChanges();
            return "ok";
      }
      catch(Exception ex)
      {
            return ex.ToString();
      }

But I just keep getting the error:
System.Runtime.InteropServices.COMException (0x80004005): Unspecified error at System.DirectoryServices.Interop.IAds.PutEx(Int32 lnControlCode, String bstrName, Object vProp) at System.DirectoryServices.PropertyValueCollection.OnClearComplete() at System.DirectoryServices.PropertyValueCollection.set_Value(Object value) at WebCreSiterWeb.CreateWinAccount.Disable()

I have the right permissions and "domain" and username" is correct!
What is wrong?
0
aleborg
Asked:
aleborg
  • 2
  • 2
1 Solution
 
vascovCommented:
Hi aleborg,

I think that you're trying to set a collection with the value of an int.

Do it like this:
                                                        |
                                                        v
myDirectory.Properties[ "UserFlags" ][ 0 ] = (int)(myDirectoryEntry.Properties["UserFlags"].Value ^ UF_ACCOUNTDISABLE);

hth

Vasco
0
 
vascovCommented:
aleborg, did this work for you ?

Vasco
0
 
aleborgAuthor Commented:
The solution was

      const int UF_ACCOUNTDISABLE = 0x0002;
      DirectoryEntry myDirectoryEntry;
      myDirectoryEntry = new DirectoryEntry("WinNT://" + domain +"/"+username+",User");
      myDirectoryEntry.Properties["UserFlags"].Add(((int)myDirectoryEntry.Properties["UserFlags"].Value) ^ UF_ACCOUNTDISABLE);
      myDirectoryEntry.CommitChanges();
0
 
aleborgAuthor Commented:
How do I close the question?
0
 
moduloCommented:
PAQed - no points refunded (of 500)

modulo
Community Support Moderator
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now