gpt4 book ai didi

c# - 使用C#枚举嵌套AD用户组

转载 作者:行者123 更新时间:2023-11-30 12:51:25 25 4
gpt4 key购买 nike

我编写了一些代码来获取组和嵌套组的所有用户。我还想确保如果组成员资格通过让第一个组成为最后一个组的成员而导致循环,则不会发生循环。

我写的代码工作正常,但有点慢。

这是我第一次尝试进行 AD 查找。

有人可以看一下并告诉我代码是否看起来不错或编码错误(或更糟),或者我的处理方式有误吗?

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using System.IO;

namespace Tester3
{
class Program3
{
public static List<string> appGroupList = new List<string>();
public static List<string> userList = new List<string>();
public static List<string> groupList = new List<string>();
public static List<string> groupChecked = new List<string>();

static void Main(string[] args)
{
// Create Output File
StreamWriter outputfile = new StreamWriter("output.txt", false);

appGroupList.Add("GLB-SBCCitrixHelpdesk-DL");
appGroupList.Add("SBC_UKBSAVIA001_PROD_ROL_Siebel");

foreach (string appGroup in appGroupList)
{
string appGroupCN = GetCN(appGroup);

GetMembers(appGroupCN);

groupChecked.Clear();
}

foreach (string item in userList)
{
Console.WriteLine(item);
outputfile.WriteLine(item);
}

outputfile.Flush();
outputfile.Close();
Console.ReadLine();
}

private static string GetCN(string group)
{
string groupCN = string.Empty;

try
{
using (DirectorySearcher search = new DirectorySearcher())
{
search.Filter = "(&(cn=" + group + ")(objectClass=group))";
search.PropertiesToLoad.Add("CN");
SearchResult result = search.FindOne();

if (result != null)
{
groupCN = result.Properties["adsPath"][0].ToString();
groupCN = groupCN.Replace("LDAP://", "");
}

return groupCN;
}
}
catch (Exception)
{
return groupCN;
}
}

public static void GetMembers(string group) // get members using the groups full cn
{
// Check if group has already been checked
if (groupChecked.Contains(group))
{
return;
}

// Add group to groupChecked list
groupChecked.Add(group);

try
{
// Connect to group object
using (DirectoryEntry groupObject = new DirectoryEntry("LDAP://" + group))
{
// Get member of group object
PropertyValueCollection col = groupObject.Properties["member"] as PropertyValueCollection;

// Loop through each member
foreach (object member in col)
{
// Connect to member object
using (DirectoryEntry memberObject = new DirectoryEntry("LDAP://" + member))
{
// Get class of member object
string memberClass = memberObject.Properties["objectClass"][1].ToString();
string memberCN = memberObject.Properties["Name"][0].ToString();

if (!groupChecked.Contains(member.ToString()))
{
if (memberClass.ToLower() == "group")
{
GetMembers(member.ToString());
}
else
{
userList.Add(memberCN);
}
}
else
{
if (memberClass.ToLower() != "group")
{
userList.Add(memberCN);
}
}
}
}
}
}
catch (Exception)
{
}
}
}
}

最佳答案

如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在这里阅读所有相关信息:

基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

if(user != null)
{
// get a user's group memberships
foreach(Principal principal in me.GetGroups())
{
GroupPrincipal gp = (principal as GroupPrincipal);

if(gp != null)
{
// do something with the group
}
}
}

新的 S.DS.AM 使得在 AD 中与用户和组一起玩真的很容易。对 .GetGroups() 的调用还可以为您处理嵌套组成员资格等所有问题 - 无需再处理这些麻烦事了!

关于c# - 使用C#枚举嵌套AD用户组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7149157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com