9

I'm attempting to find a User by username in Active Directory.

This works:

const string Domain = "SLO1.Foo.Bar.biz";
const string Username = "sanderso";

PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext,  Username);

This does not:

const string Domain = "SLO1.Foo.Bar.biz";
const string Container = "CN=Users,DC=SLO1,DC=Foo,DC=Bar,DC=biz";
const string Username = "sanderso";

PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain, Container);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, Username);

I receive the error message:

There is no such object on the server.

here's a screenshot of my ActiveDirectory setup:

enter image description here

I've also tried using the following Container:

const string Container = "OU=Users,DC=SLO1,DC=Foo,DC=Bar,DC=biz";

this was equally unsuccessful.

How can I specify my container while accessing the 'Users' container? I'm trying to do this as an initial, simple setup before introducing a lookup with more complicated requirements. So, I'd rather not settle for the simple solution because I am going to have to troubleshoot this anyway, I believe.

Sean Anderson
  • 27,963
  • 30
  • 126
  • 237
  • Sean have you tried passing the PrincipleContext with just the Domain Name ..? – MethodMan Jan 07 '13 at 23:14
  • DJ KRAZE - I remember speaking to you a few days ago. Yes, I did do that and yes, it works. But, if you'll read my bottom paragraph of this question -- I am working on a more complicated connection string for an outside customer after this and I want to make sure I'm capable of passing in a Container parameter successfully. – Sean Anderson Jan 07 '13 at 23:15
  • where is the `LDAP://` protion in the string above shouldn't it be something like `LDAP://OU=Users,DC=SLO1,DC=Foo,DC=Bar,DC=biz` – MethodMan Jan 07 '13 at 23:24
  • Hmm, something like that might be necessary. I'm getting an unknown error when I try just LDAP://, but I will play around with it for a while. Thanks! – Sean Anderson Jan 07 '13 at 23:29
  • 1
    not a problem Sean.. I work on LDAP and AD for past 10 yrs now.. let me know what you find.. – MethodMan Jan 07 '13 at 23:31

1 Answers1

10

I figured it out :)

First, I used the following software to ensure that I was generating the proper container string:

http://www.ldapbrowser.com/download.htm

This confirmed that my string was pretty much correct, aside from missing a port, but it just needed some fussing.

The correct usage is:

const string Domain = "SLO1.Foo.Bar.biz:389";
const string Container = @"DC=Foo,DC=Bar,DC=biz";
const string Username = @"sanderso";
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain, Container);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext,  username);
Sean Anderson
  • 27,963
  • 30
  • 126
  • 237
  • 1
    I hope that I was able to help you to figure out in getting your Answer Sean.. you can also Accept your answer glad I could help O totally forgot about :389 I must have been really tired last night how could I have over looked that..LOL – MethodMan Jan 08 '13 at 16:09
  • But Users is a default container, your answer doesn't specify how to name the container. something like *const string Container = @"CN=Interns,DC=Foo,DC=Bar,DC=biz";* – wruckie Jan 06 '14 at 19:55