如何在Asp.Net中获取用户的AD用户组?

我需要能够获得用户所在组的列表,但我需要看到一个/部分/全部以下属性:

  • 专有名称
  • 名称
  • CN
  • SAM帐户

我现在拥有的是某种名称,但不是上面的任何名称(名称似乎很接近,但并不是所有名称都正确匹配。这就是我正在使用的:

ArrayList groups = new ArrayList(); foreach (System.Security.Principal.IdentityReference group in System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups) groups.Add(group.Translate(typeof(System.Security.Principal.NTAccount))); 

就像我说的,上面的工作,但不会得到我的程序所需的正确名称(上面指定的名称)。 我需要这个能够匹配我在调用我的域中的所有组时获得的列表:

 DirectoryEntry dirEnt = new DirectoryEntry("LDAP://my_domain_controller"); DirectorySearcher srch = new DirectorySearcher(dirEnt); srch.Filter = "(objectClass=Group)"; var results = srch.FindAll(); 

您不能一步完成此操作,因为组也是具有属性的单独AD条目。

因此,在第一次运行中,您应该获得用户所在的组名称并将其填入某种列表中。

第二步是遍历所有组名并逐个查询它们以获取组属性(如distinguishedname等)并将其收集到某种结构中。