0
Answer

How to create User and properties in Active Directory.

Pravat Sharma

Pravat Sharma

17y
3.9k
1

Hi All, 

          I am writing a web application that will create user and properties in Active Directory. when i am trying to create a user and peoperties in AD, getting below error. Please help me out.

Getting Error Summary

--------------------------------

Server Error in '/AccessingTasks' Application.
--------------------------------------------------------------------------------

The requested operation did not satisfy one or more constraints associated with the class of the object. (Exception from HRESULT: 0x80072014)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.DirectoryServices.DirectoryServicesCOMException: The requested operation did not satisfy one or more constraints associated with the class of the object. (Exception from HRESULT: 0x80072014)

Source Error:


Line 162:        SetProperty(newuser, "userPrincipalName", login);
Line 163:        SetProperty(newuser, "mail", email);
Line 164:        newuser.CommitChanges();
Line 165:        /// 3. Set password
Line 166:      //  SetPassword(newuser.Path);
 

Source File: d:\TempToBeDeleted\AccessingTasks\LoginPage.aspx.cs    Line: 164

Stack Trace:


[DirectoryServicesCOMException (0x80072014): The requested operation did not satisfy one or more constraints associated with the class of the object. (Exception from HRESULT: 0x80072014)]
   System.DirectoryServices.DirectoryEntry.CommitChanges() +112
   LoginPage.CreateNewUser(String employeeID, String name, String login, String email, String group) in d:\TempToBeDeleted\AccessingTasks\LoginPage.aspx.cs:164
   LoginPage.Button1_Click(Object sender, EventArgs e) in d:\TempToBeDeleted\AccessingTasks\LoginPage.aspx.cs:132
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +96
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +116
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3838

My Code:-

1    protected void Button1_Click(object sender, EventArgs e)
2        {
3            CreateNewUser("001", "Pravat", "pravatsharma", "[email protected]", "User");
4        }
5    
6    public static DirectoryEntry GetDirectoryEntry()
7        {
8            DirectoryEntry de = new DirectoryEntry();
9            de.Path = "LDAP://192.165.168.55:389/CN=FDSCUSER,CN=Users,DC=FDSC,DC=net";
10           de.Username = @"fdsc.net\FDSCUSER";
11           de.Password = "pksharma";
12           return de;
13       }
14   
15   
16   public void CreateNewUser(string employeeID, string name, string login, string email, string group)
17       {
18           
19           DirectoryEntry de = GetDirectoryEntry();
20           DirectoryEntries users = de.Children;
21           DirectoryEntry newuser = users.Add("CN=" + login, "user");
22           
23           SetProperty(newuser, "FirstName", fname);
24           SetProperty(newuser, "LastName", lname);
25           SetProperty(newuser, "Activities1", onlyread);
26           SetProperty(newuser, "Activities1", readandwrite);
27           SetProperty(newuser, "SAMAccountName", login);
28           SetProperty(newuser, "userPrincipalName", login);
29           SetProperty(newuser, "mail", email);
30           newuser.CommitChanges();
31   	newuser.Close();
32           de.Close();
33       }
34   
35   
36   public static void SetProperty(DirectoryEntry de, string PropertyName, string PropertyValue)
37   {
38           if (PropertyValue != null)
39           {
40               if (de.Properties.Contains(PropertyName))
41               {
42                   de.Properties[PropertyName][0] = PropertyValue;
43               }
44               else
45               {
46                   de.Properties[PropertyName].Add(PropertyValue);
47               }
48           }
49   }
 Thanks & Regards,

 Pravat Sharma