?
Solved

Setting extensionAttribute1 using DirectoryEntry c#

Posted on 2010-08-13
2
Medium Priority
?
3,106 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 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 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 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