enable a user account in Active Directory?

  • Thread starter Thread starter Angelo
  • Start date Start date
A

Angelo

Hi,
I've been trying to create a user in Active Directory. So far this works,
however default the user is disabled. I've been trying so much code to
enable the user, but without success, perhaps you could help me.

Here's the code:
--------------------------

System.DirectoryServices.DirectorySearcher DSESearcher = new
System.DirectoryServices.DirectorySearcher();
string RootDSE=DSESearcher.SearchRoot.Path;
const long ADS_UF_ACCOUNTDISABLE = 0x2;

RootDSE=RootDSE.Insert(7,"ou=sharepoint_ou,");
DirectoryEntry myDE = new DirectoryEntry(RootDSE);
DirectoryEntries myEntries = myDE.Children;
try
{
// creating the user
DirectoryEntry myDirectoryEntry = myEntries.Add("CN="+"testje", "user");
myDirectoryEntry.Properties["givenname"].Value="firstname";
myDirectoryEntry.Properties["sn"].Value="achternaam";
myDirectoryEntry.Properties["displayname"].Value="dispname";
myDirectoryEntry.Properties["mail"].Value="(e-mail address removed)";
myDirectoryEntry.Properties["userPrincipalName"].Value = "loginname";
myDirectoryEntry.Properties["name"].Value = "username";
myDirectoryEntry.Properties["samAccountName"].Value = "loginname"; //pre
win2000 account
myDirectoryEntry.Properties["HomeDirectory"].Value = homedir;
myDirectoryEntry.CommitChanges();

// here i try to set the password
DirectoryEntry myDirectoryEntry1 = myEntries.Find("CN="+"testje",
"user");
myDirectoryEntry1.Properties["userPassword"].Value = "Igfdsfgn12345";
myDirectoryEntry1.Properties["Description"].Value = "desc";
myDirectoryEntry1.CommitChanges();

// here i try to enable the account, this gives an error.
DirectoryEntry usr = new
DirectoryEntry("LDAP://CN=testje,OU=sharepoint_ou,DC=sharepoint,DC=myserver,
DC=nl");
int val = (int) usr.Properties["userAccountControl"].Value;
usr.Properties["userAccountControl"].Value = val &
~ADS_UF_ACCOUNTDISABLE;
usr.CommitChanges();
---------------------------

I only get messages like:
"the server is unwilling to process the request"

I also tried:
myDirectoryEntry.Properties["userAccountControl"].Add(0x200);

I know that I first need to set the password before I can enable an account,
that's why my code looks like this. I'm out of ideas...

Does anyone have a solution for me?
Thanks in advance
Angelo
 
Back
Top