NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

Updating an LDAP Property in C#

Thursday, June 11th, 2009

C# is very powerful for working with LDAP and Active Directory. You are often presented with a situation in which you want to update the property of a value from an LDAP record, and I figured I would share with doing so as there are really two cases which have to be considered: the case with a single-valued attribute, and the case with a multi-valued (array) attribute.

Without further ado, I present to you the code:

DirectoryEntry entry = new DirectoryEntry("LDAP://DC=mydomain,DC=com");
DirectorySearcher searcher = new DirectorySearcher(entry);

// This will find the directory entry by a distinguished name, although you could use any filter here
searcher.Filter = "(&(distinguishedName=" + DN + "))";

// Only find the first matching record
SearchResult result = searcher.FindOne();
if (result != null)
{
DirectoryEntry updater = new DirectoryEntry();

// Set the path to the DN found by the searcher
updater.Path = result.Path;

// Username and password with write access
updater.Username = "UsernameWithWriteAccess";
updater.Password = "PasswordForUsername";

// This will update a single-valued property
updater.Properties["myproperties"].Value = "something here";

// This will add a value to a multi-valued property
updater.Properties["multival"].Add("myvalue");

// Commit the changes
updater.CommitChanges();
}

Pretty simple, but very useful and very powerful.

LDAPin' Tom Out.

Tags

CSharp LDAP

Related Blogs

Enumerating all attributes of an element and adding them to a dictionary using LINQ with Lambda Expressions
Retrieving data from SharePoint SOAP Requests using LINQ
Programatically Retrieving an Assembly's PublicKeyToken through a PowerShell CmdLet
SharePoint Web Services, .NET 3.5, and Authentication Issues
Fixing "Error in loading DLL" in SharePoint

Comments

Lance Robinson said on Thursday, June 11th, 2009 @ 10:29 AM

So much easier with IPWorks its not even funny.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations