2017-07-13 64 views
1

我想從Active Directory中獲取用戶的部門。我有以下類:我可以使用PrincipalContext從Active Directory中獲取用戶部門嗎()

public class DomainContext 
{ 
     public string DisplayName { get; set; } 
     public string Name { get; set; } 
     public string SamAccountName { get; set; } 
     public string DistinguishedName { get; set; } 
     public string UserPrincipalName { get; set; } 
     public string Department { get; set; } 
} 

然後使用下面的方法,我能得到的用戶名,顯示名稱,SAM帳戶,等....

public override void getUserDepts(SPItemEventProperties properties) 
{ 
    base.ItemUpdating(properties); 

    string[] offices = new string[] { "OfficeA", "OfficeB", "OfficeC" }; 

    string ADServerName = "*****"; 
    string ADusername = "******"; 
    string ADpassword = "*****"; 

    using (var context = new PrincipalContext(ContextType.Domain, ADServerName, ADusername, ADpassword)) 
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context))) 
    { 
     var searchResults = searcher.FindAll(); 

     foreach (Principal p in searchResults) 
     { 
      if (p.DistinguishedName.ToLower().Contains(offices[0].ToLower()) || p.DistinguishedName.ToLower().Contains(offices[1].ToLower()) || p.DistinguishedName.ToLower().Contains(offices[2].ToLower()))) 
      { 
       DomainContext dc = new DomainContext(); 
       dc.DisplayName = p.DisplayName; 
       dc.UserPrincipalName = p.UserPrincipalName; 
       dc.Name = p.Name; 
       dc.SamAccountName = p.SamAccountName; 
       dc.DistinguishedName = p.DistinguishedName; 
       // dc.Department = p. **CAN NOT GET THE DEPARTMENT NAME** 
      } 
     } 
    } 
} 

,但我無法找到我如何獲得部門名稱。

+0

是[this](https://stackoverflow.com/questions/14278274/how-i-get-active-directory-user-properties-with-system- directoryservices-account)你在找什麼? – jAC

+0

這可能更有用 - https://stackoverflow.com/questions/1785751/how-to-get-company-and-department-from-active-directory-given-a-userprincipa –

回答

1

您可以通過檢查其基礎屬性訪問部門的DirectoryEntry

var property = "department"; 
var directoryEntry = p.GetUnderlyingObject() as DirectoryEntry; 
if (directoryEntry.Properties.Contains(property)) { 
    dc.Department = directoryEntry.Properties[property].Value.ToString(); 
} 

你可以考慮上述轉換成一個擴展方法,以減少重複的代碼。

我編譯的用戶屬性的列表到一個類爲常數

public static class ADUserProperties 
{ 
    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 static string GetProperty(this DirectoryEntry directoryEntry , string propertyName, int index = 0) { 
    if (directoryEntry.Properties.Contains(propertyName) && index > -1 && index < directoryEntry.Properties[propertyName].Count) { 
     return oDE.Properties[propertyName][index].ToString(); 
    } else { 
     return string.Empty; 
    } 
} 

public static string GetProperty(this Principal principal, string property) { 
    var directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry; 
    return directoryEntry.GetProperty(property); 
} 

然後,這將使你的代碼進行更新,以

dc.Department = p.GetProperty(ADUserProperties.DEPARTMENT); 
相關問題