Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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?

Here is our production environment type.
• IIS 7
• ASP.NET 3.5 (C#)
• Active Directory
• Windows Server 2008 R2


THANKS IN ADVANCE!!!

C#
  #region SetPassword
  /// <summary>
  /// This function is used to set user password
  /// </summary>
  /// <param name="path"></param>
  /// <param name="userPassword"></param>
  /// <remarks>

/// </remarks>
  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


Add user snippet

C#
#region AddUser

    /// <summary>
    /// This function is used to add user to active directory
    /// </summary>
    /// <param name="adPath">Active Directory</param>
    /// <returns>directory entry object</returns>
    /// <remarks>
    /// </remarks>
    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
Posted
Updated 12-Apr-11 1:29am
v4
Comments
Nithin Sundar 12-Apr-11 7:20am    
Edited for readability and moved normal text outside code block.
Manfred Rudolf Bihy 12-Apr-11 7:30am    
Edit: Adjusted pre tags lang attribute for proper syntax highlighting.

Looks like you should pass oDENewUser to SetPassword instead of path and use that.

Good luck!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900