Hi All,
We are able to create new user successfully on the active directory but the SetPassword method is taking around 1.5 minutes to complete the process. Below is the code of the snippet of SetPassword. Is there any better approach to set the password of new user?
#region SetPassword
///
/// This function is used to set user password
///
///
///
///
///
internal static void SetPassword(string path, string userPassword)
{
if (_logger.IsDebugEnabled)
_logger.Debug("ADHelper.cs : Enter SetPassword");
try
{
using (DirectoryEntry usr = GetDirectoryEntry(path))
{
object ret = usr.Invoke("SetPassword", userPassword);
usr.CommitChanges();
usr.Close();
}
if (_logger.IsDebugEnabled)
_logger.Debug("ADHelper.cs : Exit SetPassword");
}
catch (Exception ex)
{
if (_logger.IsErrorEnabled)
_logger.Error("ADHelper.cs : Exception occurred in SetPassword. Message: ", ex);
throw ex;
}
}
#endregion
Here is our production environment type.
• IIS 7
• ASP.NET 3.5 (C#)
• Active Directory
• Windows Server 2008 R2
Add user snippet
#region AddUser
///
/// This function is used to add user to active directory
///
/// Active Directory
///
///
///
public static void AddUser(ADUser adUser)
{
if (_logger.IsDebugEnabled)
_logger.Debug("ADHelper.cs : Enter AddUser");
// Local variables
DirectoryEntry oDE = null;
DirectoryEntry oDENewUser = null;
DirectoryEntries oDEs = null;
try
{
oDE = GetDirectoryEntry(GetADPath(Constants.EnvironmentType.PROD, adUser.UserType));
// 1. Create user account
oDEs = oDE.Children;
oDENewUser = oDEs.Add(string.Format("{0}=", Constants.ADAttributes.CN) + adUser.UserName, "user");
// 2. Set properties
SetProperty(oDENewUser, Constants.ADAttributes.givenName, adUser.FirstName);
SetProperty(oDENewUser, Constants.ADAttributes.sn, adUser.LastName);
SetProperty(oDENewUser, Constants.ADAttributes.mail, adUser.Email);
SetProperty(oDENewUser, Constants.ADAttributes.sAMAccountName, adUser.UserName);
oDENewUser.CommitChanges();
// 3. Set password
SetPassword(oDENewUser.Path, adUser.Password);
// 4. Enable account
EnableAccount(oDENewUser);
oDENewUser.Close();
oDE.Close();
if (_logger.IsDebugEnabled)
_logger.Debug("ADHelper.cs : Exit AddUser");
}
catch (Exception ex)
{
if (_logger.IsErrorEnabled)
_logger.Error("ADHelper.cs : Exception occurred in AddUser. Message: ", ex);
throw ex;
}
finally
{
if (oDENewUser != null)
{
oDENewUser.Dispose();
oDENewUser = null;
}
if (oDEs != null)
{
oDEs = null;
}
if (oDE != null)
{
oDE.Dispose();
oDE = null;
}
}
}
#endregion
THANKS IN ADVANCE!!!

Sam HobbsRank #107 · 20.5K reputationPosted Apr 12, 2011, 6:12 PM EDT
I see you are logging debug messages using _logger.Debug; do that to help determine where the time is going. Just add a time to the messages so you can see what time that each message is done. Do that to determine whgat is taking time.
Mahesh ChandRank #1 · 393.3K reputationPosted Apr 12, 2011, 6:31 PM EDT
shrikant kesharwani11 reputationPosted Apr 15, 2011, 1:45 AM EDT
it is taking time in invoke method.
Sam HobbsRank #107 · 20.5K reputationPosted Apr 15, 2011, 2:14 AM EDT
I don't know anything about Active Directory. My guess is that someone that is familiar with it will need to ask you some more questions, but I will leave that for them to do.