前言
近期项目上通过.Net操作AD用户,获取AD用户后根据相关属性获取用户基本信息,如邮箱、显示名、电话等
DirectoryEntry Properties List
objectClass=top;person;organizationalPerson;user
cn=x1
sn=LastName
c=PL
l=City
st=State
title=Job title
description=Description
postalCode=Zip
postOfficeBox=POBox
physicalDeliveryOfficeName=Office
telephoneNumber=123456779
givenName=FirstName
distinguishedName=CN=x1,CN=Users,DC=helpdesk,DC=wat,DC=edu
instanceType=4
whenCreated=2012-11-27 21:37:37
whenChanged=2012-12-11 21:33:51
displayName=DisplayName
uSNCreated=System.__ComObject
uSNChanged=System.__ComObject
co=Poland
department=Department
company=Company
streetAddress=Street
name=x1
objectGUID=System.Byte[]
userAccountControl=66048
badPwdCount=0
codePage=0
countryCode=616
badPasswordTime=System.__ComObject
lastLogoff=System.__ComObject
lastLogon=System.__ComObject
pwdLastSet=System.__ComObject
primaryGroupID=513
objectSid=System.Byte[]
accountExpires=System.__ComObject
logonCount=1
sAMAccountName=x1
sAMAccountType=805306368
userPrincipalName=x1@helpdesk.wat.edu
objectCategory=CN=Person,CN=Schema,CN=Configuration,DC=helpdesk,DC=wat,DC=edu
dSCorePropagationData=1601-01-01 00:00:00
lastLogonTimestamp=System.__ComObject
mail=mail@mail.com
homePhone=1236456654654659
mobile=800800800
nTSecurityDescriptor=System.__ComObject
.Net获取属性代码
示例1:
DirectoryEntry deUser = new DirectoryEntry(path);
foreach (var prop in deUser.Properties)
{
//if user.Properties["company"] is not set on this user then
//it will not be available here although 'company' is
//a property defined for the user class
}
//How do I get to the list of all available properties using
//deUserSchema as below
DirectoryEntry deUserSchema = deUser.SchemaEntry();
示例2:
String myADSPath = "LDAP://onecity/CN=Users,DC=onecity,DC=corp,DC=fabrikam,DC=com";
// Creates an Instance of DirectoryEntry.
DirectoryEntry myDirectoryEntry=new DirectoryEntry(myADSPath, UserName, SecurelyStoredPassword);
// Gets the SchemaEntry of the ADS object.
DirectoryEntry mySchemaEntry = myDirectoryEntry.SchemaEntry;
if (string.Compare(mySchemaEntry.Name,"container") == 0)
{
foreach(DirectoryEntry myChildDirectoryEntry in myDirectoryEntry.Children)
{
//...do what you need
}
}