Click here to Skip to main content
6,634,665 members and growing! (15,004 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate

How to add a new user using DirectoryServices?

By Softomatix

An article describing the use of DirectoryServices namespace classes in .NET on how to add a new user in a machine or a domain.
C#, Windows, .NET 1.0, Visual Studio, Dev
Posted:19 Feb 2003
Views:166,880
Bookmarked:38 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 4.28 Rating: 3.96 out of 5
1 vote, 8.3%
1
1 vote, 8.3%
2
1 vote, 8.3%
3
1 vote, 8.3%
4
8 votes, 66.7%
5

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


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

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 40 (Total in Forum: 40) (Refresh)FirstPrevNext
GeneralCreating Group Pinmembersreejith ss nair0:38 30 Jan '08  
Generalactivedirectory user profile PinmemberJayesh Talsaniya2:17 21 May '07  
GeneralHow to delete user? PinmemberGx Lam7:12 19 Oct '06  
GeneralHow can I grant Administrative right to the newly created User? PinmemberAtif Bashir1:26 4 Aug '06  
GeneralSet Primary Group PinmemberSirLoric12:48 4 Sep '05  
Generalwindows 2003 PinmemberQuinton Viljoen3:29 15 Jun '05  
General"General access denied error\r\n" Pinmemberdmh18121:48 19 May '05  
GeneralRe: "General access denied error\r\n" PinsussNaresg10:16 23 Aug '05  
GeneralRe: "General access denied error\r\n" Pinmembernano2k22:22 21 Nov '05  
GeneralRe: "General access denied error\r\n" Pinmembermohdmo200523:45 16 Jul '07  
GeneralRe: "General access denied error\r\n" PinmemberMember 18463283:11 29 Sep '08  
AnswerRe: "General access denied error\r\n" Pinmemberdbbtvlzfpz23:27 19 Oct '08  
GeneralAdd user to AD too slow Pinmembersonnv22:39 2 Mar '05  
Generaladd user problem Pinmembersanaz_21:48 14 Aug '04  
GeneralAuthentication issue PinsussAnonymous3:47 5 Mar '04  
Generaland in vb.net ? Pinmembertrollibus4:37 20 Jan '04  
GeneralRe: and in vb.net ? Pinsussanonymous10:05 11 Feb '04  
GeneralClickety PinmemberColin Angus Mackay6:00 6 Oct '04  
Generalget current user's password Pinmembercronosxfiles1:32 9 Dec '03  
GeneralExchange Pinmembercandan15:11 18 Aug '03  
GeneralGroup membership Pinmemberandy.ainsworth@forbessolicitors.co.uk0:18 6 Aug '03  
GeneralRe: Group membership Pinmembercerda23:07 29 Mar '04  
QuestionRe: Group membership PinmemberAtif Bashir0:19 8 Aug '06  
GeneralSetting a UserFlag to Don't Expire a Password Pinsussoptimistck5:46 18 Mar '03  
GeneralRe: Setting a UserFlag to Don't Expire a Password PinmemberOptimistck6:39 18 Mar '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Feb 2003
Editor: Nishant Sivakumar
Copyright 2003 by Softomatix
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project