Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Setting extensionAttribute1 using DirectoryEntry c#

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

609 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