Click here to Skip to main content
11,708,892 members (74,195 online)
Click here to Skip to main content

How to add a new user using DirectoryServices?

, 19 Feb 2003 227.8K 42
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

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
GeneralCreating Group Pin
sreejith ss nair29-Jan-08 23:38
membersreejith ss nair29-Jan-08 23:38 
Generalactivedirectory user profile Pin
Jayesh Talsaniya21-May-07 1:17
memberJayesh Talsaniya21-May-07 1:17 
QuestionHow to delete user? Pin
Gx Lam19-Oct-06 6:12
memberGx Lam19-Oct-06 6:12 
QuestionHow can I grant Administrative right to the newly created User? Pin
Atif Bashir4-Aug-06 0:26
memberAtif Bashir4-Aug-06 0:26 
GeneralSet Primary Group Pin
SirLoric4-Sep-05 11:48
memberSirLoric4-Sep-05 11:48 
Generalwindows 2003 Pin
Quinton Viljoen15-Jun-05 2:29
memberQuinton Viljoen15-Jun-05 2:29 
General"General access denied error\r\n" Pin
dmh18119-May-05 20:48
memberdmh18119-May-05 20:48 
GeneralRe: "General access denied error\r\n" Pin
Naresg23-Aug-05 9:16
sussNaresg23-Aug-05 9:16 
GeneralRe: "General access denied error\r\n" Pin
nano2k21-Nov-05 21:22
membernano2k21-Nov-05 21:22 
GeneralRe: "General access denied error\r\n" Pin
mohdmo200516-Jul-07 22:45
membermohdmo200516-Jul-07 22:45 
GeneralRe: "General access denied error\r\n" Pin
Member 184632829-Sep-08 2:11
memberMember 184632829-Sep-08 2:11 
AnswerRe: "General access denied error\r\n" Pin
dbbtvlzfpz19-Oct-08 22:27
memberdbbtvlzfpz19-Oct-08 22:27 
GeneralAdd user to AD too slow Pin
sonnv2-Mar-05 21:39
membersonnv2-Mar-05 21:39 
Generaladd user problem Pin
sanaz_14-Aug-04 20:48
membersanaz_14-Aug-04 20:48 
GeneralAuthentication issue Pin
Anonymous5-Mar-04 2:47
sussAnonymous5-Mar-04 2:47 
Questionand in vb.net ? Pin
trollibus20-Jan-04 3:37
membertrollibus20-Jan-04 3:37 
AnswerRe: and in vb.net ? Pin
anonymous11-Feb-04 9:05
sussanonymous11-Feb-04 9:05 
GeneralClickety Pin
Colin Angus Mackay6-Oct-04 5:00
memberColin Angus Mackay6-Oct-04 5:00 
Generalget current user's password Pin
cronosxfiles9-Dec-03 0:32
membercronosxfiles9-Dec-03 0:32 
GeneralExchange Pin
candan18-Aug-03 14:11
membercandan18-Aug-03 14:11 
GeneralGroup membership Pin
andy.ainsworth@forbessolicitors.co.uk5-Aug-03 23:18
memberandy.ainsworth@forbessolicitors.co.uk5-Aug-03 23:18 
GeneralRe: Group membership Pin
cerda29-Mar-04 22:07
membercerda29-Mar-04 22:07 
QuestionRe: Group membership Pin
Atif Bashir7-Aug-06 23:19
memberAtif Bashir7-Aug-06 23:19 
GeneralSetting a UserFlag to Don't Expire a Password Pin
optimistck18-Mar-03 4:46
sussoptimistck18-Mar-03 4:46 
GeneralRe: Setting a UserFlag to Don't Expire a Password Pin
Optimistck18-Mar-03 5:39
memberOptimistck18-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 | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 20 Feb 2003
Article Copyright 2003 by Softomatix
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid