Asked by:
Why this code don't work ? add user to ad and enable account

Question
-
HI,
Can You tell me, why this code not eneble user account after added it to AD ?
this function add user, and close cmd window with error, but in AD i can see just added user account.
im run this code on WS 2k8 ee DC
public void CreateUserAccount(string connectionPrefix , string userName, string userPassword)
{
try
{
DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
DirectoryEntry newUser = dirEntry.Children.Add("CN=" + userName, "user");
newUser.Properties["samAccountName"].Value = userName;
newUser.CommitChanges();
newUser.Invoke("SetPassword", new object[] { userPassword });
newUser.CommitChanges();
//enable user account
newUser.Properties["userAccountControl"].Value = ~0x200;
newUser.CommitChanges();
newUser.Close();
dirEntry.Close();
}
catch (System.DirectoryServices.DirectoryServicesCOMException e)
{
Console.WriteLine("{}",e);
Console.ReadLine();
}
}
#cheers!Monday, April 27, 2009 10:55 AM
All replies
-
You should put more TRY / CATCH statements between every CommitChanges() so that you can see which part spawns the error
What Error does it actually produce?
- Belgian Unified Communications Community : http://www.pro-exchange.be -Tuesday, April 28, 2009 11:05 PM -
HIHave you done it?if yes send me the codeI also want to enable user that i made in OCSAnd hoping for your help as soon as possibleThanks in advanceRegardsSohaib QaziFriday, August 7, 2009 12:45 PM
-
Take a look at this article for some background on manually controlling AD attributes to enable OCS users:
http://blogs.pointbridge.com/Blogs/schertz_jeff/Pages/Post.aspx?_ID=29
Jeff Schertz, PointBridge | MVP | MCITP: Enterprise Messaging | MCTS: OCSFriday, August 7, 2009 1:12 PMModerator -
JeffJeff i want to enable user for OCS that i made just in ADFor that i need code in C#I am not getting help from the code given above or inNothing is helpful for meSo help me to take out of thisThanks in advanceRegardsSohaib QaziSaturday, August 8, 2009 8:57 AM
-
HiI have done itIf you needs any help tell meRegardsSohaib QaziMonday, August 10, 2009 6:06 AM