Modifying LDAP attributes using C#

I have been working on a small application that can bind to the directory and modify an attribute.  So far I have been able to bind correctly and retrieve a Directory Entry.  But I am unable to modify an attribute.  This is not a rights issue it seems to be a coding issue and my understanding of C#.  Does someone have an example I can see to modify an attribute.  What I have tried so far is

SearchResult result = mySearcher.FindOne();
result.Properties["HomeDirectory"][0]= "bob";

Thanks
darkstagg@yahoo.com
darkstaggAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ihenryCommented:
SearchResult object contains just some readonly data for you to use, not to modify. Use DirectoryEntry object from the SearchResult object and modify it, like so

DirectoryEntry de = result.GetDirectoryEntry();
de.Properties["HomeDirectory"].Value = "myHomeDirectory";
de.CommitChanges();
0
darkstaggAuthor Commented:
Im still a bit stuck on this.  Although closer thanks.  It appears now when I try to modify the object in question I am unable to because I am bound to the directory in a different way.  What I am trying to do is bind to the AD with my admin account and modify an attribute for another user.  Here is my code in general so far.

DirectoryEntry entry = new DirectoryEntry  "LDAP://domain.com","CN=admin,OU=Administrators,OU=ca,OU=is,DC=domain,DC=com","password");
                  
      myDirectorySearcher = new DirectorySearcher("cn=bob");
      SearchResult result = myDirectorySearcher.FindOne();

      DirectoryEntry clientEntry = result.GetDirectoryEntry();

      clientEntry.Properties["homeDirectory"].Value = "bob";
      clientEntry.CommitChanges();

When I do this it errors out and appears to be unable to access the value.  When I use the debugger I can see result contains the appropriate attributes.

Thanks again
Darkstagg
0
ihenryCommented:
You're passing the user name "CN=admin,OU=Administrators,OU=ca,OU=is,DC=domain,DC=com" in DN format to the DirectoryEntry ctor, in order to get this to work you must use SSL binding option in the DirectoryEntry object. To avoid SSL binding you can use user name in NT format like "yourDomain\userName" or in UPN format such as "userName@yourDomain.com" .

In order to make the search to be more efficient you would need to include objectClass and objectCategory in the filter, like so:
(&(objectClass=user)(objectCategory=person)(cn=bob))

And  try to avoid using FindOne method as it might give you serious memory leak problem in production later. You can consider using "using" statement and FindAll method as a replacement.

               using (SearchResultCollection src = myDirectorySearcher.FindAll())
               {
                    if (src.Count > 0)
                    {
                         SearchResult sr = src[0];
                         // code continue here...

For any exception occur, post the exception stack trace here.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

darkstaggAuthor Commented:
My apologies for being somewhat dim on this I have a strong understanding of LDAP and you are right on adding the filter and how I should do the search.  But I am not clear on the bind process and then searching correctly for another object.  Can you give me an example of this.

Pseudo code

bind to directory with Admin account.
search for a user (not the admin user).
modify the homedirectory attribute.
commit the change to the directory.

again sorry for not fully understanding I greatly appreciate the help

Darkstagg
0
darkstaggAuthor Commented:
I believe I have it now thanks so much for the help
0
ihenryCommented:
Bob,
I believe my comments have helped the asker and put him to the right direction.

-- Henry
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.