Find Windows Logged in First and Last name for Intranet

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
This should be simple but I guess not... I can retrieve the Windows ID when they get to the Web/Intranet form, but then I need to retrieve the information from the Domain or Active Directory. How can I properly query the active directory domain?
There are so many web answers, and nothing is clear. I want to call the FindName(xx"useraccount"xx) and get the answer.
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2 <font face="Consolas" size="2

</font></font><font face="Consolas" size="2

</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public DirectoryEntry GetDirectoryEntry()
{
DirectoryEntry de = <span style="color:Blue; new DirectoryEntry();
de.Path = <span style="color:#A31515; "LDAP://DC=xxxxxxx.com,DC=com";
de.AuthenticationType = AuthenticationTypes.Secure;
<span style="color:Blue; return de;
}

<span style="color:Blue; public <span style="color:Blue; bool UserExists(<span style="color:Blue; string username)
{
DirectoryEntry de = GetDirectoryEntry();
DirectorySearcher deSearch = <span style="color:Blue; new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = <span style="color:#A31515; "(&(objectClass=user) (cn=" + username + <span style="color:#A31515; "))";
SearchResultCollection results = deSearch.FindAll();
<span style="color:Blue; return results.Count > 0;
}

<span style="color:Blue; public String FindName(String userAccount)
{
DirectoryEntry entry = GetDirectoryEntry();
String account = userAccount.Replace(<span style="color:#A31515; @"Domain", <span style="color:#A31515; "");
<span style="color:Blue; try
{
DirectorySearcher search = <span style="color:Blue; new DirectorySearcher(entry);
search.Filter = <span style="color:#A31515; "(SAMAccountName=" + account + <span style="color:#A31515; ")";
search.PropertiesToLoad.Add(<span style="color:#A31515; "displayName");
SearchResult result = search.FindOne();
<span style="color:Blue; if (result != <span style="color:Blue; null)
{
<span style="color:Blue; return result.Properties[<span style="color:#A31515; "displayname"][0].ToString();
}
<span style="color:Blue; else
{
<span style="color:Blue; return <span style="color:#A31515; "Unknown User";
}
}
<span style="color:Blue; catch (Exception ex)
{
<span style="color:Blue; string debug = ex.Message;
<span style="color:Blue; return <span style="color:#A31515; "";
}
}
[/code]
<font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff

</font></font></font><font face="Consolas" size="2" color="#0000ff <font face="Consolas" size="2" color="#0000ff

</font></font><font face="Consolas" size="2" color="#0000ff

</font>

<span style="font-family:Consolas; font-size:x-small <hr class="sig -Robert

View the full article
 
Back
Top