By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

enable a user account in Active Directory?

P: n/a
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="em********@emdf.com";
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=myserv er,
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

Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
never mind, found the solution:

http://www.codeguru.com/Csharp/Cshar...cle.php/c6021/

thanks for reading!
Angelo

"Angelo" <AngeloNNSPMHouben_et_hotmail.com> wrote in message
news:eF*************@TK2MSFTNGP15.phx.gbl...
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="em********@emdf.com";
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=myserv er, 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

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.