kvigor
asked on
How to Return all user attributes given their samaccountname
I'm trying to return the First Name and Last Name of a user by only providing the samaccountname to the search filter.
protected void Page_Load(object sender, EventArgs e)
{
GetActiveADUsers("LDAP://adBox.company.com", "CN=samaccountname=domain\\user");
}
public void GetActiveADUsers(string ldapString, string adSearchFilter)
{
DirectoryEntry de = new DirectoryEntry(ldapString,"domain\\username","password");
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.SearchScope = SearchScope.Subtree; //Including the sub OU's
deSearch.Filter = adSearchFilter;
SearchResultCollection result = deSearch.FindAll();
//Get User Array Here
for (int x = 0; x < result.Count; x++)
{
string firstLastName = Convert.ToBoolean(result[x].Properties["name"].Count > 0) ? result[x].Properties["name"][0].ToString() : "";
System.Web.HttpContext.Current.Response.Write(firstLastName + "<br />");
}
}
ASKER
I'll try this solution in the morning.
ASKER
I get the following error with your code:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Source Error:
Line 41: //string lastName = result.Properties["sn"][0] .ToString( );
Line 42: //string fullName = result.Properties["name"][ 0].ToStrin g();
Line 43: string displayName = result.Properties["display name"][0]. ToString() ;
Line 44:
Line 45: System.Web.HttpContext.Cur rent.Respo nse.Write( displayNam e + "<br />");
I'm using the exact code you posted???
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept
Source Error:
Line 41: //string lastName = result.Properties["sn"][0]
Line 42: //string fullName = result.Properties["name"][
Line 43: string displayName = result.Properties["display
Line 44:
Line 45: System.Web.HttpContext.Cur
I'm using the exact code you posted???
Suggests that displayName is blank (in AD). Does name return correctly?
Chris
ASKER
All properties returned blank: sn, name, and displayName. I commented them out one by one thinking the same thing but I know I have a name and displayname as I tested my account.
Okay, I must have done something wrong then. One sec.
Chris
ASKER
I inserted the domain\username here:
(samaccountname=domain\\us ername)
(samaccountname=domain\\us
That won't work.
sAMAccountName in the directory is just the username, domain doesn't get a look in there.
Chris
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So what am I not getting, I'm using this to get the samaccountname:
username = User.Identity.Name.ToStrin g();
and I'm passing in username var to here:
GetActiveADUsers("LDAP://a dBox.compa ny.com", "HERE"); is there way this will work
I'm actually going to get the fullname/display name from a different namespace because the directoryservices namespace may not be as user friendly as I need it to be.
username = User.Identity.Name.ToStrin
and I'm passing in username var to here:
GetActiveADUsers("LDAP://a
I'm actually going to get the fullname/display name from a different namespace because the directoryservices namespace may not be as user friendly as I need it to be.
As long as the "Name" value above is only the user name, and not "domain\username" then it's fine. Although with the code above it's expecting you to pass a valid LDAP filter (perhaps like "(samAccountName=" + username + ")").
That'll break a bit if you use the same user name in the authentication string for the directory entry and expect it to have the domain name there. If that is the case I would say pass user and domain as separate values, or build the filter in the function, parsing the username out of "domain\user".
Chris
ASKER
ohhhh! So Sorry that DID work I didn't understand what you meant by:
ID: 24764619 "domain doesn't get a look in there."
So I removed the "domain\" and all returned well. Thanks you saved me alot of time.
Your Solution like Ambien it Worked like a Dream : ^)
ID: 24764619 "domain doesn't get a look in there."
So I removed the "domain\" and all returned well. Thanks you saved me alot of time.
Your Solution like Ambien it Worked like a Dream : ^)
ASKER
User was perfect in solution.
Hey :)
I would do this...
Chris
Open in new window