Attempting to validate id against Active Directory

  • Thread starter Ted Vreeland via DotNetMonster.com
  • Start date
T

Ted Vreeland via DotNetMonster.com

Hi Folks,

I've got a intranet ASP.NET page that takes a user id (e.g. 'DoeJ') passed in and attempts to get the First and Last name of the user from the Active Directory. I'm running into some issues and am hoping someone can look at it and tell me whats wrong. I've gotten several different errors (Referal from server, unspecified error, etc). Any assistance would be greatly appreciated.

Best Regards,
Ted

CODE:
-----------------
DirectoryEntry _de = new DirectoryEntry( "LDAP://rootDSE" );
string _DomainPath = _de.Properties["DefaultNamingContext"][0].ToString();
DirectoryEntry _root = new DirectoryEntry( _DomainPath );
DirectorySearcher _src = new DirectorySearcher( _root);
_src.Filter = "(&(objectCategory=Person)(SAMAccountName=" + Request["DomainId"] + ")";

_src.PropertiesToLoad.Add( "givenname" );
_src.PropertiesToLoad.Add( "sn" );
_src.PropertiesToLoad.Add( "mail" );

SearchResult _res = _src.FindOne();

firstName = _res.Properties["givenname"][0].ToString();
lastName = _res.Properties["sn"][0].ToString();
 
O

Ollie Riches

Ted,

Questions about AD are best posted in the microsoft.public.adsi.general
newsgroup where the two Joe's will be most help, but in answer to your
problem i think this should work

DirectoryEntry _root = new DirectoryEntry (@"LDAP://rootdse");
string path = _root.Invoke("GET","defaultNamingContext").ToString();
DirectoryEntry _de = new DirectoryEntry ("LDAP://" + path);
DirectorySearcher _src = new DirectorySearcher( _de);
_src.Filter = "sAMAccountName=XXXXXXX";
_src.PropertiesToLoad.Add( "givenname" );
_src.PropertiesToLoad.Add( "sn" );
_src.PropertiesToLoad.Add( "mail" );
SearchResult _res = _src.FindOne();
firstName = _res.Properties["givenname"][0].ToString();
lastName = _res.Properties["sn"][0].ToString();


HTH

Ollie Riches

Ted Vreeland via DotNetMonster.com said:
Hi Folks,

I've got a intranet ASP.NET page that takes a user id (e.g. 'DoeJ') passed
in and attempts to get the First and Last name of the user from the Active
Directory. I'm running into some issues and am hoping someone can look at
it and tell me whats wrong. I've gotten several different errors (Referal
from server, unspecified error, etc). Any assistance would be greatly
appreciated.
Best Regards,
Ted

CODE:
-----------------
DirectoryEntry _de = new DirectoryEntry( "LDAP://rootDSE" );
string _DomainPath = _de.Properties["DefaultNamingContext"][0].ToString();
DirectoryEntry _root = new DirectoryEntry( _DomainPath );
DirectorySearcher _src = new DirectorySearcher( _root);
_src.Filter = "(&(objectCategory=Person)(SAMAccountName=" + Request["DomainId"] + ")";

_src.PropertiesToLoad.Add( "givenname" );
_src.PropertiesToLoad.Add( "sn" );
_src.PropertiesToLoad.Add( "mail" );

SearchResult _res = _src.FindOne();

firstName = _res.Properties["givenname"][0].ToString();
lastName = _res.Properties["sn"][0].ToString();
 
T

Ted Vreeland via DotNetMonster.com

I found the problem - I needed the <identity impersonate="true" /> setting added to the web.config - that allowed the AD query to run under the identity of the logged in user.

Thanks for the help anyway.

TV
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top