检测活动目录对象的正确方法属于用户或组?

时间:2016-10-06 13:07:56

标签: c# active-directory directoryservices

检测活动目录对象的正确方法是什么属于组或用户?这是我在C#中处理的方式:

foreach (SearchResult sr in src)
{
   if (sr.Properties["objectclass"].Contains("person") && sr.Properties["objectclass"].Contains("user"))
   {
      // USER ?
   }
   if (sr.Properties["objectclass"].Contains("group"))
   {
      // GROUP ?
   }
}

1 个答案:

答案 0 :(得分:2)

if (sr != null)
{
    if(sr.Properties["objectCategory"] != null)
    {
       // objectType will be "Person" or "Group" (or something else entirely)
       string objectType = sr.Properties["objectCategory"][0].ToString();
       if (objectType == "Person")
       { 
          //It's a user
       }
       if (objectType == "Group")
       { 
          //It's a Group
       }
    }
}

取自:How to determine the type (AD User vs. AD Group) of an account?