Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Article

How to add a new user using DirectoryServices?

Rate me:
Please Sign up or sign in to vote.
3.93/5 (12 votes)
19 Feb 20032 min read 279.7K   43   40
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.

C#
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


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

Comments and Discussions

 
GeneralCreating Group Pin
sreejith ss nair29-Jan-08 23:38
sreejith ss nair29-Jan-08 23:38 
Generalactivedirectory user profile Pin
Jayesh Talsaniya21-May-07 1:17
Jayesh Talsaniya21-May-07 1:17 
QuestionHow to delete user? Pin
Gx Lam19-Oct-06 6:12
Gx Lam19-Oct-06 6:12 
QuestionHow can I grant Administrative right to the newly created User? Pin
Atif Bashir4-Aug-06 0:26
Atif Bashir4-Aug-06 0:26 
GeneralSet Primary Group Pin
Robert M Greene4-Sep-05 11:48
Robert M Greene4-Sep-05 11:48 
Generalwindows 2003 Pin
Quinton Viljoen15-Jun-05 2:29
Quinton Viljoen15-Jun-05 2:29 
General"General access denied error\r\n" Pin
Daniel Hover19-May-05 20:48
Daniel Hover19-May-05 20:48 
GeneralRe: "General access denied error\r\n" Pin
Naresg23-Aug-05 9:16
Naresg23-Aug-05 9:16 
GeneralRe: "General access denied error\r\n" Pin
nano2k21-Nov-05 21:22
nano2k21-Nov-05 21:22 
GeneralRe: "General access denied error\r\n" Pin
mohdmo200516-Jul-07 22:45
mohdmo200516-Jul-07 22:45 
GeneralRe: "General access denied error\r\n" Pin
Member 184632829-Sep-08 2:11
Member 184632829-Sep-08 2:11 
AnswerRe: "General access denied error\r\n" Pin
Pravinc198419-Oct-08 22:27
Pravinc198419-Oct-08 22:27 
GeneralAdd user to AD too slow Pin
sonnv2-Mar-05 21:39
sonnv2-Mar-05 21:39 
Generaladd user problem Pin
Member 63691514-Aug-04 20:48
Member 63691514-Aug-04 20:48 
GeneralAuthentication issue Pin
Anonymous5-Mar-04 2:47
Anonymous5-Mar-04 2:47 
Questionand in vb.net ? Pin
Member 74919920-Jan-04 3:37
Member 74919920-Jan-04 3:37 
AnswerRe: and in vb.net ? Pin
Anonymous11-Feb-04 9:05
Anonymous11-Feb-04 9:05 
GeneralClickety Pin
Colin Angus Mackay6-Oct-04 5:00
Colin Angus Mackay6-Oct-04 5:00 
Generalget current user's password Pin
cronosxfiles9-Dec-03 0:32
cronosxfiles9-Dec-03 0:32 
GeneralExchange Pin
candan18-Aug-03 14:11
professionalcandan18-Aug-03 14:11 
GeneralGroup membership Pin
andyainsworth5-Aug-03 23:18
andyainsworth5-Aug-03 23:18 
GeneralRe: Group membership Pin
cerda29-Mar-04 22:07
cerda29-Mar-04 22:07 
QuestionRe: Group membership Pin
Atif Bashir7-Aug-06 23:19
Atif Bashir7-Aug-06 23:19 
GeneralSetting a UserFlag to Don't Expire a Password Pin
Optimistck18-Mar-03 4:46
Optimistck18-Mar-03 4:46 
GeneralRe: Setting a UserFlag to Don't Expire a Password Pin
Optimistck18-Mar-03 5:39
Optimistck18-Mar-03 5:39 

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

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