none
DirectoryEntry set properties to user RRS feed

  • Question

  •  

    Hello All,

    Can you please guide me to set the properties for directoryEntry the user added.

    I am adding user in following way.

                         DirectoryEntry AD = new DirectoryEntry("WinNT://" +

                        "******"  +",computer");//Environment.MachineName        

                    DirectoryEntry NewUser = AD.Children.Add("User5", "user");

    now want to set the properties here like mail , first name ,last name.

               
    Friday, December 1, 2017 10:48 AM

Answers

All replies

  • You should use LDAP instead of WinNT. The below link has examples (e.g. CreateNewUser) that should get you started:

    https://www.codeproject.com/Articles/90142/Everything-in-Active-Directory-via-C-NET-Using


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by shital diwate Friday, December 1, 2017 3:25 PM
    Friday, December 1, 2017 2:01 PM
  • Thank you for your reply.

    Considering your suggestion now I tried with following code 

                            

     using (var pc = new PrincipalContext(ContextType.Domain,"domain server"))
                    {
                        using (var up = new UserPrincipal(pc))
                        {
                            try
                            {
                                up.SamAccountName = "User1ytuytu3";
                                up.EmailAddress = "User100@abc.com";
                                up.SetPassword("test@123");
                                up.VoiceTelephoneNumber = "1236547899";
                                up.Surname = "abc";
                                up.Name = "abc1";
                                up.MiddleName = "xyz";
                                up.Enabled = true;
                                up.ExpirePasswordNow();
                                up.Save();
                            }
                            catch (Exception ex) { }
                           

                        }

    first time it worked properly but when I executed this for next time it gave me the following exception.

    "The object already exists."

     



    Friday, December 1, 2017 2:56 PM
  • Did you change the SamAccountName the next time you added a new user?

    BTW, there is code from that link that checks to see if the user already exists.


    Paul ~~~~ Microsoft MVP (Visual Basic)


    Friday, December 1, 2017 3:15 PM
  • Done.

    Here  SamAccountName  and name should be unique and I was repeating the name while testing.

     

    • Marked as answer by shital diwate Friday, December 1, 2017 3:25 PM
    Friday, December 1, 2017 3:25 PM