在Asp.Net MVC 4 Windows身份验证模式下,是否可以获取有关当前用户的更多信息(如姓名和家人)?
答案 0 :(得分:0)
获得用户名后,您可以使用Active Directory API获取用户的其他属性。您可以使用以下代码:
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"));
}
这可以是GetProperty方法:
private string GetProperty(SearchResult searchResult, string PropertyName)
{
if (searchResult.Properties.Contains(PropertyName))
{
return searchResult.Properties[PropertyName][0].ToString();
}
else
{
return string.Empty;
}
}
和propertyName可以是:
之类的值