获取当前域名:
System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain();
从LDAP获取目录条目和域名:
DirectoryEntry de = new DirectoryEntry(string.Format("LDAP://{0}", domain));
从ActiveDirectoryMembershipProvider ActiveDirectoryMembershipUser对象中获取SID:
ActiveDirectoryMembershipUser user = (ActiveDirectoryMembershipUser)Membership.GetUser();
var sid = (SecurityIdentifier)user.ProviderUserKey;
从SecurityIdentifier获取用户名:
(NTAccount)sid.Translate(typeof(NTAccount));
在激活目录中使用域目录条目和用户名完成目录搜索。
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = string.Format("(SAMAccountName={0})", username);
search.PropertiesToLoad.Add("Name");
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("company");
search.PropertiesToLoad.Add("homePhone");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("givenName");
search.PropertiesToLoad.Add("lastLogon");
search.PropertiesToLoad.Add("userPrincipalName");
search.PropertiesToLoad.Add("st");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("telephoneNumber");
search.PropertiesToLoad.Add("postalCode");
SearchResult result = search.FindOne();
if (result != null)
{
foreach (string key in result.Properties.PropertyNames)
{
// Each property contains a collection of its own
// that may contain multiple values
foreach (Object propValue in result.Properties[key])
{
outputString += key + " = " + propValue + ".<br/>";
}
}
}
根据您的活动目录中的数据,您将获得不同的输出响应。
这里有一个网站,它包含我需要的所有用户属性:http://www.winzero.ca/Active-Directory-users.htm。