Click here to Skip to main content
Click here to Skip to main content

How to add a new user using DirectoryServices?

, 19 Feb 2003
Rate this:
Please Sign up or sign in to vote.
An article describing the use of DirectoryServices namespace classes in .NET on how to add a new user in a machine or a domain.

Introduction

In our previous article, How to get full name of logged in user, we showed how you can get full name of a user in a given domain or machine. You can extend that idea to obtain any information you want for a given user. This artcile will describe how you can add a new user account into your domain or machine using .Net DirectoryServices classes. We will be using WinNT provider for illustrations in this article. But you can extend the examples to use LDAP or AD providers.

Details

Here are the key steps that you will need to perform to create new account.

  • Create a new DirectoryEntry object and specify the machine name as the path.
  • User accounts are created as nodes corrresponding to User schema class in Active Directory. Therefore we will add a new DirectoryEntry object in Children collection of the machine. The key thing to rememeber will be that when you add new entry, make sure that the schema class name is User.
  • When you add a new node in Children collection, it will return you the newly created object. At this stage the information has not been added to your machine or active directory tree.
  • Now you can set all the values that you need to set for a given account. Following is the list of property names that you can set for the account.
    • UserFlags
    • MaxStorage
    • PasswordAge
    • PasswordExpired
    • LoginHours
    • FullName
    • Description
    • BadPasswordAttempts
    • LastLogin
    • HomeDirectory
    • LoginScript
    • Profile
    • HomeDirDrive
    • Parameters
    • PrimaryGroupID
    • Name
    • MinPasswordLength
    • MaxPasswordAge
    • MinPasswordAge
    • PasswordHistoryLength
    • AutoUnlockInterval
    • LockoutObservationInterval
    • MaxBadPasswordsAllowed
    • RasPermissions
    • objectSid

    For more information on these properties please read Active Directory Services Interface(ADSI) section in Microsoft Platform SDK.

  • You must have noticed from the above list that there is no property to set or get user password value. Operating system does not give access to clear text password value. So we can't expect and property or method to get it. In ADSI, IAdsUser interface provides SetPassword method to set a user's password. This is where Invoke method of DirectoryEntry class comes handy. So we call Invoke to set the password value. The Invoke method can be used to call native methods on underlying active directory objects. There is one important thing to remeber when you set a user's password value. If you are using LDAP provider, then the user account should already have been created in the system by calling CommitChanges or SetInfo method. But WinNT provider does not have this restriction. You can set password value without commiting the changes first.
  • The last step would be to actually create the account in the machine or domain. This is done by calling CommitChanges method on newly added DirectoryEntry object.

The following code demonstrates all the steps that we described above.

private void AddUser(string strDoamin, string strLogin, string strPwd)
{
    DirectoryEntry obDirEntry = null;
    try
    {
        obDirEntry = new DirectoryEntry("WinNT://" + strDoamin);
        DirectoryEntries entries = obDirEntry.Children;
        DirectoryEntry obUser = entries.Add(strLogin, "User");
        obUser.Properties["FullName"].Add("Amigo");
        object obRet = obUser.Invoke("SetPassword", strPwd);
        obUser.CommitChanges();
    }
    catch (Exception ex)
    {
        Trace.Warn(ex.Message);
    }
}

Please feel free to send your suggestions directly to us at softomatix@pardesiservices.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Softomatix
Web Developer
United States United States
To learn more about us, Please visit us at http://www.netomatix.com

Comments and Discussions

 
GeneralCreating Group Pinmembersreejith ss nair29-Jan-08 23:38 
Generalactivedirectory user profile PinmemberJayesh Talsaniya21-May-07 1:17 
QuestionHow to delete user? PinmemberGx Lam19-Oct-06 6:12 
QuestionHow can I grant Administrative right to the newly created User? PinmemberAtif Bashir4-Aug-06 0:26 
GeneralSet Primary Group PinmemberSirLoric4-Sep-05 11:48 
Generalwindows 2003 PinmemberQuinton Viljoen15-Jun-05 2:29 
General"General access denied error\r\n" Pinmemberdmh18119-May-05 20:48 
GeneralRe: "General access denied error\r\n" PinsussNaresg23-Aug-05 9:16 
GeneralRe: "General access denied error\r\n" Pinmembernano2k21-Nov-05 21:22 
GeneralRe: "General access denied error\r\n" Pinmembermohdmo200516-Jul-07 22:45 
GeneralRe: "General access denied error\r\n" PinmemberMember 184632829-Sep-08 2:11 
AnswerRe: "General access denied error\r\n" Pinmemberdbbtvlzfpz19-Oct-08 22:27 
GeneralAdd user to AD too slow Pinmembersonnv2-Mar-05 21:39 
Generaladd user problem Pinmembersanaz_14-Aug-04 20:48 
GeneralAuthentication issue PinsussAnonymous5-Mar-04 2:47 
Questionand in vb.net ? Pinmembertrollibus20-Jan-04 3:37 
AnswerRe: and in vb.net ? Pinsussanonymous11-Feb-04 9:05 
GeneralClickety PinmemberColin Angus Mackay6-Oct-04 5:00 
Generalget current user's password Pinmembercronosxfiles9-Dec-03 0:32 
GeneralExchange Pinmembercandan18-Aug-03 14:11 
GeneralGroup membership Pinmemberandy.ainsworth@forbessolicitors.co.uk5-Aug-03 23:18 
GeneralRe: Group membership Pinmembercerda29-Mar-04 22:07 
QuestionRe: Group membership PinmemberAtif Bashir7-Aug-06 23:19 
GeneralSetting a UserFlag to Don't Expire a Password Pinsussoptimistck18-Mar-03 4:46 
GeneralRe: Setting a UserFlag to Don't Expire a Password PinmemberOptimistck18-Mar-03 5:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 20 Feb 2003
Article Copyright 2003 by Softomatix
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid