Solved

Setting extensionAttribute1 using DirectoryEntry c#

Posted on 2010-08-13
2
2,891 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 71

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

724 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