CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

AD GetDirectoryEntry on DirectorySearch result <> new DirectoryEntry (Security hits back)

In my last post I described some of my troubles working with Active directory entries from code. Here's another one which increased my baldness.

I described using a Directory search object, creating a DirectoryEntry out of that and setting the password to validate the entry. Like this:

const string MemberOf = "memberof";

   

DirectorySearcher ds = new DirectorySearcher();

ds.Filter = "samaccountname=" + userName;

ds.PropertiesToLoad.Add(MemberOf);

SearchResult sr = ds.FindOne();

DirectoryEntry adsEntry = sr.GetDirectoryEntry();

adsEntry.Password = passWord;

try

{

   object o = adsEntry.NativeObject;

}

   catch (DirectoryServicesCOMException ex)

{

   // Not a valid account

}

Alas this does not always work in a real life domain. It does work when you are validating your own account under which you are logged in. But it does not work for any other account. What does work is creating a new directoryentry from scratch using exactly the same credentials.

DirectoryEntry adsEntry = new DirectoryEntry("", userName, passWord);

try

{

    object o = adsEntry.NativeObject;

}

catch (DirectoryServicesCOMException ex)

{

    // Not a valid account

}

Don't ask me why, without a doubt it's intended as security. Crossing the border from coding into the world of IT management just stays hard.


Published Dec 14 2006, 04:47 AM by pvanooijen
Filed under:

Comments

pvanooijen said:

Ah, yes.

Now I see the light.

What I hadn't realized is that you do need credentials to access allmost every kind of AD object. Also from code.

# November 21, 2007 3:20 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News