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**
}
}
}
}
,但我無法找到我如何獲得部門名稱。
是[this](https://stackoverflow.com/questions/14278274/how-i-get-active-directory-user-properties-with-system- directoryservices-account)你在找什麼? – jAC
這可能更有用 - https://stackoverflow.com/questions/1785751/how-to-get-company-and-department-from-active-directory-given-a-userprincipa –