2012-04-03 72 views

回答

1

添加「公司」到的DirectorySearcher的PropertiesToLoad屬性:

var ds = new DirectorySearcher(ldapRoot); 
ds.PropertiesToLoad.Add("company"); 
var directoryEntry = ds.FindOne(); 
... 
Console.WriteLine(directoryEntry.Properties["company"]); 
0

下面是類我幾年前寫的拉本信息。

我還沒有深入到代碼多年,所以它必須工作得很好。

希望你能從中獲得一些用處。

class ADProperties { 
    public const string OBJECTCLASS = "objectClass"; 
    public const string CONTAINERNAME = "cn"; 
    public const string LASTNAME = "sn"; 
    public const string COUNTRYNOTATION = "c"; 
    public const string CITY = "l"; 
    public const string STATE = "st"; 
    public const string TITLE = "title"; 
    public const string POSTALCODE = "postalCode"; 
    public const string PHYSICALDELIVERYOFFICENAME = "physicalDeliveryOfficeName"; 
    public const string FIRSTNAME = "givenName"; 
    public const string MIDDLENAME = "initials"; 
    public const string DISTINGUISHEDNAME = "distinguishedName"; 
    public const string INSTANCETYPE = "instanceType"; 
    public const string WHENCREATED = "whenCreated"; 
    public const string WHENCHANGED = "whenChanged"; 
    public const string DISPLAYNAME = "displayName"; 
    public const string USNCREATED = "uSNCreated"; 
    public const string MEMBEROF = "memberOf"; 
    public const string USNCHANGED = "uSNChanged"; 
    public const string COUNTRY = "co"; 
    public const string DEPARTMENT = "department"; 
    public const string COMPANY = "company"; 
    public const string PROXYADDRESSES = "proxyAddresses"; 
    public const string STREETADDRESS = "streetAddress"; 
    public const string DIRECTREPORTS = "directReports"; 
    public const string NAME = "name"; 
    public const string OBJECTGUID = "objectGUID"; 
    public const string USERACCOUNTCONTROL = "userAccountControl"; 
    public const string BADPWDCOUNT = "badPwdCount"; 
    public const string CODEPAGE = "codePage"; 
    public const string COUNTRYCODE = "countryCode"; 
    public const string BADPASSWORDTIME = "badPasswordTime"; 
    public const string LASTLOGOFF = "lastLogoff"; 
    public const string LASTLOGON = "lastLogon"; 
    public const string PWDLASTSET = "pwdLastSet"; 
    public const string PRIMARYGROUPID = "primaryGroupID"; 
    public const string OBJECTSID = "objectSid"; 
    public const string ADMINCOUNT = "adminCount"; 
    public const string ACCOUNTEXPIRES = "accountExpires"; 
    public const string LOGONCOUNT = "logonCount"; 
    public const string LOGINNAME = "sAMAccountName"; 
    public const string SAMACCOUNTTYPE = "sAMAccountType"; 
    public const string SHOWINADDRESSBOOK = "showInAddressBook"; 
    public const string LEGACYEXCHANGEDN = "legacyExchangeDN"; 
    public const string USERPRINCIPALNAME = "userPrincipalName"; 
    public const string EXTENSION = "ipPhone"; 
    public const string SERVICEPRINCIPALNAME = "servicePrincipalName"; 
    public const string OBJECTCATEGORY = "objectCategory"; 
    public const string DSCOREPROPAGATIONDATA = "dSCorePropagationData"; 
    public const string LASTLOGONTIMESTAMP = "lastLogonTimestamp"; 
    public const string EMAILADDRESS = "mail"; 
    public const string MANAGER = "manager"; 
    public const string MOBILE = "mobile"; 
    public const string PAGER = "pager"; 
    public const string FAX = "facsimileTelephoneNumber"; 
    public const string HOMEPHONE = "homePhone"; 
    public const string MSEXCHUSERACCOUNTCONTROL = "msExchUserAccountControl"; 
    public const string MDBUSEDEFAULTS = "mDBUseDefaults"; 
    public const string MSEXCHMAILBOXSECURITYDESCRIPTOR = "msExchMailboxSecurityDescriptor"; 
    public const string HOMEMDB = "homeMDB"; 
    public const string MSEXCHPOLICIESINCLUDED = "msExchPoliciesIncluded"; 
    public const string HOMEMTA = "homeMTA"; 
    public const string MSEXCHRECIPIENTTYPEDETAILS = "msExchRecipientTypeDetails"; 
    public const string MAILNICKNAME = "mailNickname"; 
    public const string MSEXCHHOMESERVERNAME = "msExchHomeServerName"; 
    public const string MSEXCHVERSION = "msExchVersion"; 
    public const string MSEXCHRECIPIENTDISPLAYTYPE = "msExchRecipientDisplayType"; 
    public const string MSEXCHMAILBOXGUID = "msExchMailboxGuid"; 
    public const string NTSECURITYDESCRIPTOR = "nTSecurityDescriptor"; 
} 

public class ADUser { 

    #region ' Properties ' 
    public string City { get; private set; } 
    public string Company { get; private set; } 
    public string Country { get; private set; } 
    public string Department { get; private set; } 
    public string EmailAddress { get; private set; } 
    public string Extension { get; private set; } 
    public string Fax { get; private set; } 
    public string FirstName { get; private set; } 
    public string HomePhone { get; private set; } 
    public string LastName { get; private set; } 
    public string UserName { get; private set; } 
    public string LoginNameWithDomain { get; private set; } 
    public string FullName { 
    get { 
    if (!String.IsNullOrEmpty(FirstName)) { 
     if (!String.IsNullOrEmpty(LastName)) { 
     return string.Format("{0} {1}", FirstName, LastName); 
     } else { 
     return FirstName; 
     } 
    } else if (!String.IsNullOrEmpty(LastName)) { 
     return LastName; 
    } else { 
     return null; 
    } 
    } 
    } 

    public string MiddleName { get; private set; } 
    public string Mobile { get; private set; } 
    public string PostalCode { get; private set; } 
    public string StreetAddress { get; private set; } 
    public string State { get; private set; } 
    public string Title { get; private set; } 
    #endregion 

    private ADUser(DirectoryEntry directoryUser) { 
    FirstName = GetProperty(directoryUser, ADProperties.FIRSTNAME); 
    MiddleName = GetProperty(directoryUser, ADProperties.MIDDLENAME); 
    LastName = GetProperty(directoryUser, ADProperties.LASTNAME); 
    UserName = GetProperty(directoryUser, ADProperties.LOGINNAME); 
    string userPrincipalName = GetProperty(directoryUser, ADProperties.USERPRINCIPALNAME); 
    string domainAddress = (!String.IsNullOrEmpty(userPrincipalName)) ? userPrincipalName.Split('@')[1] : null; 
    string domainName = (!String.IsNullOrEmpty(domainAddress)) ? domainAddress.Split('.')[0] : null; 
    LoginNameWithDomain = String.Format(@"{0}\{1}", domainName, UserName); 
    StreetAddress = GetProperty(directoryUser, ADProperties.STREETADDRESS); 
    City = GetProperty(directoryUser, ADProperties.CITY); 
    State = GetProperty(directoryUser, ADProperties.STATE); 
    PostalCode = GetProperty(directoryUser, ADProperties.POSTALCODE); 
    Country = GetProperty(directoryUser, ADProperties.COUNTRY); 
    Company = GetProperty(directoryUser, ADProperties.COMPANY); 
    Department = GetProperty(directoryUser, ADProperties.DEPARTMENT); 
    HomePhone = GetProperty(directoryUser, ADProperties.HOMEPHONE); 
    Extension = GetProperty(directoryUser, ADProperties.EXTENSION); 
    Mobile = GetProperty(directoryUser, ADProperties.MOBILE); 
    Fax = GetProperty(directoryUser, ADProperties.FAX); 
    EmailAddress = GetProperty(directoryUser, ADProperties.EMAILADDRESS); 
    Title = GetProperty(directoryUser, ADProperties.TITLE); 
    } 

    private static string GetProperty(DirectoryEntry userDetail, string propertyName) { 
    if (userDetail.Properties.Contains(propertyName)) { 
     return userDetail.Properties[propertyName][0].ToString(); 
    } else { 
     return null; 
    } 
    } 

    public static ADUser GetUser(DirectoryEntry directoryUser) { 
    return new ADUser(directoryUser); 
    } 

    public override string ToString() { 
    if (!String.IsNullOrEmpty(EmailAddress)) { 
     return string.Format("{0} <{1}>", FullName, EmailAddress); 
    } else { 
     return FullName; 
    } 
    } 
} 
+1

如果您已經檢索到directoryEntry中的公司屬性,這將起作用。如果您使用尚未加載公司屬性的DirectoryEntry對象調用ADUser構造函數,則看起來它將返回null。 – Jaymz 2012-04-03 14:41:57

+0

嗯...也許。我還有另一堂課與這兩個小班合作,但我的另一堂課充滿了OP可能不想要的個人公司特定信息。我會把它留在這裏作爲參考,但承認你的版本是更好的答案。 – jp2code 2012-04-03 14:44:35

+0

感謝Jaymz和jp2code, 我製作了GetProperty方法並開始工作。 我想知道是否有可能尋找通過價值的公司,而不是比較價值? 因爲這樣,我必須比較所有用戶的值,直到比較返回true。 Regards, Víctor – 2012-04-03 21:21:31