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
DirectoryEntryobject 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
DirectoryEntryobject inChildrencollection 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
Childrencollection, 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.
UserFlagsMaxStoragePasswordAgePasswordExpiredLoginHoursFullNameDescriptionBadPasswordAttemptsLastLoginHomeDirectoryLoginScriptProfileHomeDirDriveParametersPrimaryGroupIDNameMinPasswordLengthMaxPasswordAgeMinPasswordAgePasswordHistoryLengthAutoUnlockIntervalLockoutObservationIntervalMaxBadPasswordsAllowedRasPermissionsobjectSid
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,
IAdsUserinterface providesSetPasswordmethod to set a user's password. This is whereInvokemethod ofDirectoryEntryclass comes handy. So we callInvoketo set the password value. TheInvokemethod 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 callingCommitChangesorSetInfomethod. 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
CommitChangesmethod on newly addedDirectoryEntryobject.
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
'System > Microsoft SharePoint' 카테고리의 다른 글
| Active Directory를 이용한 Form Authentication의 접근 방향 (0) | 2007.09.11 |
|---|---|
| MOSS Form based authentication (0) | 2007.09.07 |
| Active Directory ㅠ_ㅠ (0) | 2007.09.06 |
| MOSS 테크넷 (0) | 2007.09.05 |
| Active directory 제어 (0) | 2007.09.05 |