locked
DirectoryEntry Set Password to Never Expire RRS feed

  • Question

  • I am using the code shown below to programmatically add a new Windows user account.

    I am having trouble with setting the User account to have the password set to Never Expire.

    I have made a few attempts, but I get an exception error "The Parameter is not correct".

    Hoping to get some assistance in making this work.

    My Code That works:

    DirectoryEntry AD = new DirectoryEntry("WinNT://"+Environment.MachineName+",computer");
    DirectoryEntry NewUser = AD.Children.Add(Name,"user");
    NewUser.Invoke("SetPassword",new object[] { Pass });
    NewUser.Invoke("Put",new object[] { "Description","Test User" });

    // Would like to add code here to set New User Account so password never expires

    NewUser.CommitChanges();
    DirectoryEntry grp;
    grp=AD.Children.Find("Administrators","group");
    if(grp!=null) { grp.Invoke("Add",new object[] { NewUser.Path.ToString() }); }

    Code I have tried:

    1)

    NewUser.Invoke("Put",new object[] { "UserFlags", 0x10000 });

    2) 

    int val = (int)NewUser.Properties["UserFlags"].Value;
    NewUser.Properties["UserFlags"].Value=val|0x10000;

    I get an exception error "The parameter is not correct".

    Thanks for any comments that can help me with this.


    • Edited by Mr Cheese Monday, April 27, 2020 12:10 PM
    Sunday, April 26, 2020 9:14 AM

Answers

  • I had made a small mistake, and the code I originally found is working.

    Error was coming from a different line of code than I thought.

    This code is working now:

    string Name = "TestUser";
    string Pass = "password";
    DirectoryEntry AD = new DirectoryEntry("WinNT://"+Environment.MachineName+",computer");
    DirectoryEntry NewUser = AD.Children.Add(Name,"user");
    NewUser.Invoke("SetPassword",new object[] { Pass });
    NewUser.Invoke("Put",new object[] { "Description","Test User" });
    NewUser.Invoke("Put",new object[] { "UserFlags", 0x10000 });
    NewUser.CommitChanges();
    DirectoryEntry grp;
    grp=AD.Children.Find("Administrators","group");
    if(grp!=null) { grp.Invoke("Add",new object[] { NewUser.Path.ToString() }); }


    • Proposed as answer by CoolDadTx Monday, April 27, 2020 12:59 PM
    • Marked as answer by Mr Cheese Tuesday, April 28, 2020 9:08 AM
    Monday, April 27, 2020 12:19 PM

All replies

  • Hi Mr Cheese,

    Thank you for posting here.

    See if the explanation in this link is helpful to you.

    The Parameter is Incorrect when trying to connect to AD using DirectoryEntry

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 27, 2020 9:00 AM
  • I had made a small mistake, and the code I originally found is working.

    Error was coming from a different line of code than I thought.

    This code is working now:

    string Name = "TestUser";
    string Pass = "password";
    DirectoryEntry AD = new DirectoryEntry("WinNT://"+Environment.MachineName+",computer");
    DirectoryEntry NewUser = AD.Children.Add(Name,"user");
    NewUser.Invoke("SetPassword",new object[] { Pass });
    NewUser.Invoke("Put",new object[] { "Description","Test User" });
    NewUser.Invoke("Put",new object[] { "UserFlags", 0x10000 });
    NewUser.CommitChanges();
    DirectoryEntry grp;
    grp=AD.Children.Find("Administrators","group");
    if(grp!=null) { grp.Invoke("Add",new object[] { NewUser.Path.ToString() }); }


    • Proposed as answer by CoolDadTx Monday, April 27, 2020 12:59 PM
    • Marked as answer by Mr Cheese Tuesday, April 28, 2020 9:08 AM
    Monday, April 27, 2020 12:19 PM
  • Hi Mr Cheese,

    I am glad to hear that.

    Please mark your final reply as an answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 28, 2020 6:17 AM