Solved

Setting extensionAttribute1 using DirectoryEntry c#

Posted on 2010-08-13
2
2,707 Views
Last Modified: 2013-12-20
Morning all,

I have been creating accounts using the DirectoryEntry in c# but now I am required to set extensionAttributes but when I try the code below I get errors saying "The attribute syntax specified to the directory service is invalid"

How can i set the extensionAttributes?


/// <summary>
        /// Create a new user account in the Active Directory
        /// Function by http://www.Kazoosoft.eu
        /// copyright Steven Church steven@kazoosoft.eu
        /// </summary>
        /// <param name="ldapPath"></param>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <param name="userPrincipalName"></param>
        /// <param name="GivenName"></param>
        /// <param name="SN"></param>
        /// <param name="profilePath"></param>
        /// <param name="homeDirectory"></param>
        /// <param name="mail"></param>
        /// <param name="mailNickname"></param>
        /// <param name="distinguishedName"></param>
        /// <param name="displayName"></param>
        /// <param name="scriptPath"></param>
        /// <param name="homeDrive"></param>
        /// <param name="extensionAttribute1"></param>
        /// <param name="extensionAttribute2"></param>
        /// <param name="extensionAttribute3"></param>
        /// <param name="extensionAttribute4"></param>
        /// <param name="extensionAttribute5"></param>
        /// <param name="extensionAttribute6"></param>
        /// <param name="extensionAttribute7"></param>
        /// <param name="extensionAttribute8"></param>
        /// <param name="extensionAttribute9"></param>
        /// <param name="extensionAttribute10"></param>
        /// <param name="extensionAttribute11"></param>
        /// <param name="extensionAttribute12"></param>
        /// <returns></returns>
        public static string CreateUserAccount(string ldapPath,
            string userName, 
            string userPassword, 
            string userPrincipalName, 
            string GivenName,
            string SN,
            string profilePath, 
            string homeDirectory, 
            string mail, 
            string mailNickname, 
            string distinguishedName, 
            string displayName,
            string scriptPath,
            string homeDrive,
            string extensionAttribute1,
            string extensionAttribute2,
            string extensionAttribute3,
            string extensionAttribute4,
            string extensionAttribute5,
            string extensionAttribute6,
            string extensionAttribute7,
            string extensionAttribute8,
            string extensionAttribute9,
            string extensionAttribute10,
            string extensionAttribute11,
            string extensionAttribute12)
            //string accountExpires, 
            //string proxyAddress
            
        {
            try
            {
                string oGUID = string.Empty;
                string connectionPrefix = "LDAP://" + ldapPath;
                //string connectionPrefix = "LDAP://IPADDRESS/OU=2010,OU=Stu,OU=Test_Area,OU=Group Test,DC=domain,DC=local";
                DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix,
                    KazooADIntegration.Properties.Settings.Default.LDAP_domain + "\\" + KazooADIntegration.Properties.Settings.Default.LDAP_username,
                    KazooADIntegration.Properties.Settings.Default.LDAP_password,
                    AuthenticationTypes.Secure);

                //DirectoryEntry dirEntry = new DirectoryEntry();
                DirectoryEntry newUser = dirEntry.Children.Add
                    ("CN=" + userName, "user");

                newUser.Properties["sAMAccountName"].Add(userName);            
                newUser.Properties["userPrincipalName"].Add(userPrincipalName);
                newUser.Properties["givenName"].Add(GivenName);
                newUser.Properties["sn"].Add(SN);
                newUser.Properties["profilePath"].Add(profilePath);
                newUser.Properties["homeDirectory"].Add(homeDirectory);
                newUser.Properties["mail"].Add(mail);
                newUser.Properties["mailNickname"].Add(mailNickname);
                newUser.Properties["distinguishedName"].Add(distinguishedName);
                newUser.Properties["displayName"].Add(displayName);
                newUser.Properties["scriptPath"].Add(scriptPath);
                newUser.Properties["homeDrive"].Add(homeDrive);

                
                // Set the Extension Attributes
                if (extensionAttribute1 != null)
                {
                    newUser.Properties["extensionAttribute1"].Add(extensionAttribute1);
                }
                if (extensionAttribute2 != null)
                {
                    newUser.Properties["extensionAttribute2"].Add(extensionAttribute2);
                }
                if (extensionAttribute3 != null)
                {
                    newUser.Properties["extensionAttribute3"].Add(extensionAttribute3);
                }
                if (extensionAttribute4 != null)
                {
                    newUser.Properties["extensionAttribute4"].Add(extensionAttribute4);
                }
                if (extensionAttribute5 != null)
                {
                    newUser.Properties["extensionAttribute5"].Add(extensionAttribute5);
                }
                if (extensionAttribute6 != null)
                {
                    newUser.Properties["extensionAttribute6"].Add(extensionAttribute6);
                }
                if (extensionAttribute7 != null)
                {
                    newUser.Properties["extensionAttribute7"].Add(extensionAttribute7);
                }
                if (extensionAttribute8 != null)
                {
                    newUser.Properties["extensionAttribute8"].Add(extensionAttribute8);
                }
                if (extensionAttribute9 != null)
                {
                    newUser.Properties["extensionAttribute9"].Add(extensionAttribute9);
                }
                if (extensionAttribute10 != null)
                {
                    newUser.Properties["extensionAttribute10"].Add(extensionAttribute10);
                }
                if (extensionAttribute11 != null)
                {
                    newUser.Properties["extensionAttribute11"].Add(extensionAttribute11);
                }
                if (extensionAttribute12 != null)
                {
                    newUser.Properties["extensionAttribute12"].Add(extensionAttribute12);
                }
                
                

                newUser.CommitChanges();
                
                oGUID = newUser.Guid.ToString();

                newUser.Invoke("SetPassword", new object[] { userPassword });
                newUser.CommitChanges();
                dirEntry.Close();
                newUser.Close();
                return "created";

                
            }

Open in new window

0
Comment
Question by:KazooSoft
2 Comments
 
LVL 70

Expert Comment

by:Chris Dent
ID: 33428524
Hi KazooSoft,

It would be worth verifying the entry it's attempting to add if possible?

I guess none of the values passed as extensionAttribute* are empty ("")? I don't think you'd catch that with the Null test, and AD won't like you for attempting it (the error you have is exactly what I'd expect to see).

Perhaps you might use:

if (!String.IsNullOrEmpty(extensionAttribute1)) {
  // Add the property
}

HTH

Chris
0
 
LVL 7

Accepted Solution

by:
jdavistx earned 500 total points
ID: 33430671
Additionally, you may want to consider passing your attributes in as a List or an array for readability?

//Assuming you passed an array called "attributes" to your function

for(int i = 0; i < attributes.Count; i++ )

{

   if( !String.IsNullOrEmpty(attributes[i])

      newUser.Properties[attributes[i]].Add(attributes[i]);

}

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Not showing page correctly 3 29
DBF to ... Converter 5 29
Variable Event ? 3 20
Not seen Link button 5 0
This article describes some very basic things about SQL Server filegroups.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now