如何为Asp.Net中的用户获取AD用户组?
时间:2020-03-06 14:20:13 来源:igfitidea点击:
我需要能够获得用户所在的组的列表,但是我需要显示以下一个/一些/全部属性:
- 专有名称
- 姓名
- cn
- samaccountname
我现在所拥有的返回的是某种名称,但上面的名称都不是(名称看起来很接近,但并非都正确匹配。这就是我正在使用的名称:
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条目。
因此,在第一次运行中,我们应该获取用户所在的组名称,并将其填写在某种列表中。
第二步是遍历所有组名,并逐一查询它们以获取组属性(例如专有名称等)并将其收集为某种结构。