Click here to Skip to main content
Licence 
First Posted 19 Feb 2003
Views 203,253
Bookmarked 42 times

How to add a new user using DirectoryServices?

By | 19 Feb 2003 | Article
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

Member

To learn more about us, Please visit us at http://www.netomatix.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCreating Group Pinmembersreejith ss nair23:38 29 Jan '08  
Generalactivedirectory user profile PinmemberJayesh Talsaniya1:17 21 May '07  
QuestionHow to delete user? PinmemberGx Lam6:12 19 Oct '06  
QuestionHow can I grant Administrative right to the newly created User? PinmemberAtif Bashir0:26 4 Aug '06  
can I grant Administrative rights to the newly created User?
 
Watch Your Thoughts for they will become your actions.
Watch Your Actions for they will become your habits.
Watch Your Habits for they will become your beliefs.
Watch Your Beliefs for they will determine your destiny.

GeneralSet Primary Group PinmemberSirLoric11:48 4 Sep '05  
Generalwindows 2003 PinmemberQuinton Viljoen2:29 15 Jun '05  
General"General access denied error\r\n" Pinmemberdmh18120:48 19 May '05  
GeneralRe: "General access denied error\r\n" PinsussNaresg9:16 23 Aug '05  
GeneralRe: "General access denied error\r\n" Pinmembernano2k21:22 21 Nov '05  
GeneralRe: "General access denied error\r\n" Pinmembermohdmo200522:45 16 Jul '07  
GeneralRe: "General access denied error\r\n" PinmemberMember 18463282:11 29 Sep '08  
AnswerRe: "General access denied error\r\n" Pinmemberdbbtvlzfpz22:27 19 Oct '08  
GeneralAdd user to AD too slow Pinmembersonnv21:39 2 Mar '05  
Generaladd user problem Pinmembersanaz_20:48 14 Aug '04  
GeneralAuthentication issue PinsussAnonymous2:47 5 Mar '04  
Questionand in vb.net ? Pinmembertrollibus3:37 20 Jan '04  
AnswerRe: and in vb.net ? Pinsussanonymous9:05 11 Feb '04  
GeneralClickety PinmemberColin Angus Mackay5:00 6 Oct '04  
Generalget current user's password Pinmembercronosxfiles0:32 9 Dec '03  
GeneralExchange Pinmembercandan14:11 18 Aug '03  
GeneralGroup membership Pinmemberandy.ainsworth@forbessolicitors.co.uk23:18 5 Aug '03  
GeneralRe: Group membership Pinmembercerda22:07 29 Mar '04  
QuestionRe: Group membership PinmemberAtif Bashir23:19 7 Aug '06  
GeneralSetting a UserFlag to Don't Expire a Password Pinsussoptimistck4:46 18 Mar '03  
GeneralRe: Setting a UserFlag to Don't Expire a Password PinmemberOptimistck5:39 18 Mar '03  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 20 Feb 2003
Article Copyright 2003 by Softomatix
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid