In an Asp.Net MVC 4 windows authentication mode, is it possible to get more information about current user (like name & family)?
Once you get the username you can use Active Directory API to get other properties of user. You can use the following code for example:
Using System.DirectoryServices;
...
DirectoryEntry entry = new DirectoryEntry("LDAP://DomainName");
DirectorySearcher dSearch = new DirectorySearcher(entry);
dSearch.PropertiesToLoad.Add("displayName");
dSearch.PropertiesToLoad.Add("cn");
dSearch.PropertiesToLoad.Add("department");
dSearch.Filter = "(&(objectClass=user)(l=" + username + "))";
foreach (SearchResult result in searcher.FindAll())
{
// Login Name
Console.WriteLine(GetProperty(result, "cn"));
// Display Name
Console.WriteLine(GetProperty(result, "displayName"));
// Department
Console.WriteLine(GetProperty(result, "department"));
}
and this can be the GetProperty method:
private string GetProperty(SearchResult searchResult, string PropertyName)
{
if (searchResult.Properties.Contains(PropertyName))
{
return searchResult.Properties[PropertyName][0].ToString();
}
else
{
return string.Empty;
}
}
and propertyName can be values like: