![](/img/trans.png)
[英]How to get json string as response using Webhook in asp.net c# Webforms
[英]How to get the members from multiple security groups using ASP.NET C# WebForms?
我正在使用以下方法来获取Active Directory中特定安全组的成员,并将它们添加到数据表中。 这工作正常,但我想对其进行修改以添加多个组的成员。 如何修改代码以包括三个特定的组?
这是我现在正在使用的:
using (var context = new PrincipalContext(ContextType.Domain, null))
{
using (var group = (GroupPrincipal.FindByIdentity(context, "Security Group 1")))
{
var users = group.GetMembers(true);
foreach (UserPrincipal user in users)
{
DirectoryEntry de = user.GetUnderlyingObject() as DirectoryEntry;
dt.Rows.Add
(
Convert.ToString(de.Properties["givenName"].Value),
Convert.ToString(de.Properties["sn"].Value),
Convert.ToString(de.Properties["mail"].Value),
Convert.ToString(de.Properties["department"].Value),
Regex.Replace((Convert.ToString(de.Properties["manager"].Value)), @"CN=([^,]*),.*$", "$1")
);
}
}
}
我还要添加“安全组2”和“安全组3”中的成员。
由于您对所有3个组都使用相同的属性,并且唯一的不同是由于成员,因此我将使其保持简单并使用一个foreach循环。
using (var context = new PrincipalContext(ContextType.Domain, null))
{
// Declare array to hold names of groups
string[] groups = new string[]{"Security Group 1", "Security Group 2", "Security Group 3"};
// Iterate through each group and perform operation
foreach (string group in groups){
// Notice, your hardcoded group name has been replaced with the group variable
using (var group = (GroupPrincipal.FindByIdentity(context, group)))
{
var users = group.GetMembers(true);
foreach (UserPrincipal user in users)
{
DirectoryEntry de = user.GetUnderlyingObject() as DirectoryEntry;
dt.Rows.Add
(
Convert.ToString(de.Properties["givenName"].Value),
Convert.ToString(de.Properties["sn"].Value),
Convert.ToString(de.Properties["mail"].Value),
Convert.ToString(de.Properties["department"].Value),
Regex.Replace((Convert.ToString(de.Properties["manager"].Value)), @"CN=([^,]*),.*$", "$1")
);
}
}
}
}
或大致符合这些原则的东西。 除非我误解了您的要求,否则这应该是解决您的问题的简单方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.