Click here to Skip to main content
15,897,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a web page through this page when I try to add a new user then users created successfully but when try resetting their password then I am getting errors’
add New user successfully
C#
public static void AddUser(ADUser adUser)
  {
      // Local variables
      DirectoryEntry oDE = null;
      DirectoryEntry oDENewUser = null;
      DirectoryEntries oDEs = null;
      try
      {
          oDE = GetDirectoryEntry(GetADPath(PROD, adUser.UserType));

          // 1. Create user account
          oDEs = oDE.Children;
          oDENewUser = oDEs.Add("CN=" + adUser.UserName, "user");

          // 2. Set properties
          SetProperty(oDENewUser, "givenName", adUser.FirstName);
          SetProperty(oDENewUser, "sn", adUser.LastName);
          SetProperty(oDENewUser, "mail", adUser.Email);
          SetProperty(oDENewUser, "sAMAccountName", adUser.UserName);
          oDENewUser.CommitChanges();
          /// 4. Enable account
          EnableAccount(oDENewUser);
          // 3. Set password
          //SetPassword(oDENewUser, adUser.Password);
          SetPassword1(oDENewUser.Path, adUser.Password);
          oDENewUser.CommitChanges();
          oDENewUser.Close();
          oDE.Close();
      }
      catch (Exception ex)
      {
          throw ex;
      }
  }

I have try the following 2 SetPassword methods but getting error.
Method 1.
C#
internal static void SetPassword1(string path, string userPassword)
   {
       //Local variables
       DirectoryEntry usr = null;

       try
       {
           usr = new DirectoryEntry();
           usr.Path = path;
           usr.AuthenticationType = AuthenticationTypes.Secure;
           object ret = usr.Invoke("SetPassword", userPassword);
           usr.CommitChanges();
           usr.Close();
       }
       catch (Exception ex)
       {
           throw ex;
       }
   }

The exception raised (Error Code 80072035: The server is unwilling to process the request)
Method 2.
C#
internal static void SetPassword(DirectoryEntry de, string userPassword)
        {
            //Local variables
            //DirectoryEntry usr = null;
            string quotePwd;
            byte[] pwdBin;
            
            try
            {
                quotePwd = String.Format(@"""{0}""", userPassword);
                pwdBin = System.Text.Encoding.Unicode.GetBytes(quotePwd);
                de.Properties["unicodePwd"].Value = pwdBin;
                de.CommitChanges();
                //usr.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

The exception raised ("Exception has been thrown by the target of an invocation.")
Is there an easy way to tell if there is a problem with changing a password?
Please reply me as soon as possible.
Thanks.
Posted
Updated 24-Jan-11 2:07am
v2
Comments
Dylan Morley 24-Jan-11 8:37am    
Possibly a problem with your password - are you sure the password you are providing conforms to the password strength policy on the domain. e.g. at least 1 capital, at least 1 number?

1 solution

If I'm reading this correctly, you're not changing the password...a new account doesn't have one to change. :)

Anyway, to create a password you need to run under an account with admin privileges. Hopefully, the IIS/asp.net account doesn't have them, so you'll need to set user & password along with the AuthenticationTypes.Secure you already have.

Honestly, you're probably better off just creating the account and have Windows ask the user for a password the first time they log on.

See here: http://www.codeproject.com/KB/system/everythingInAD.aspx#40a[^]

and here http://www.primaryobjects.com/CMS/Article66.aspx[^]
 
Share this answer
 

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