使用C#和#x27;s Active Directory中的计算机主体,根据用户选择用计算机填充列表框
我的C#应用程序与active directory通信,当我选择一个用户时,我可以看到他所属的组,如果我选择该组,我可以看到该组中的用户。现在,我想看看每个用户所关联的计算机的新需求使用C#和#x27;s Active Directory中的计算机主体,根据用户选择用计算机填充列表框,c#,active-directory,C#,Active Directory,我的C#应用程序与active directory通信,当我选择一个用户时,我可以看到他所属的组,如果我选择该组,我可以看到该组中的用户。现在,我想看看每个用户所关联的计算机的新需求 private void FillComputers() { computers.Items.Clear(); PrincipalContext PrincipalContext1 = new PrincipalContext(ContextType.Domain
private void FillComputers()
{
computers.Items.Clear();
PrincipalContext PrincipalContext1 = new PrincipalContext(ContextType.Domain);
ComputerPrincipal cp = new ComputerPrincipal(PrincipalContext1);
PrincipalSearcher search = new PrincipalSearcher(cp);
foreach (var cpn in search.FindAll())
{
computers.Items.Add(cpn.SamAccountName);
}
}
为我提供所有计算机如何将所选用户作为参数传递给上面的计算机???提前感谢您的建议
例如,当我有用户时,我必须找出他们所属的组,我就这样做了
private void fillmembersof()
{
Groups.Items.Clear();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
String input = user.Text;
string username = input.Split(new char[] { '[', ']' })[1];
// define a "query-by-example" principal - here, we search for any UserPrincipal
UserPrincipal usr = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username);
if (usr != null)
{
foreach (var group in usr.GetGroups())
{
Groups.Items.Add(group.Name);
}
usr.Dispose();
}
ctx.Dispose();
}
以下内容用所有用户填充列表框:
private void filluser()
{
// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// define a "query-by-example" principal - here, we search for any UserPrincipal
UserPrincipal qbeUser = new UserPrincipal(ctx);
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach (var found in srch.FindAll())
{
try
{
// do whatever here
UserPrincipal foundUser = found as UserPrincipal;
if (foundUser != null)
{
foundUser.IsAccountLockedOut();
user.Items.Add(foundUser.GivenName + " " + foundUser.Surname + " " + "[" + foundUser.SamAccountName + "]");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
为了用用户所属的组填充另一个列表框,我这样做了
private void fillmembersof()
{
Groups.Items.Clear();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
String input = user.Text;
string username = input.Split(new char[] { '[', ']' })[1];
// define a "query-by-example" principal - here, we search for any UserPrincipal
UserPrincipal usr = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username);
if (usr != null)
{
foreach (var group in usr.GetGroups())
{
Groups.Items.Add(group.Name);
}
usr.Dispose();
}
ctx.Dispose();
}
而要根据所选用户填写上述文本框,我执行了以下操作:
private void user_SelectedIndexChanged(Object sender, EventArgs e)
{
fillmembersof();
**FillComputers();** -> i want to be able to do the same for computers the user has access to?????
提前感谢您的宝贵建议 在Active Directory中,我不相信计算机与特定用户有关联。我从未见过两者之间有任何联系。@lordjeb尝试获取能够访问所选计算机的用户怎么样?因此,之前我希望为每个用户提供一个计算机列表,但可以做相反的事情,例如,每台计算机都有多个用户,现在我们有了一个计算机列表,我们可以看到其中的用户吗?其中一些计算机也是服务器。在域系统上,任何用户都可以使用他们有权使用的任何计算机。一旦计算机加入域,如果计算机允许“域用户”组访问,则任何用户都可以访问它。这并不意味着他们会这么做。要确定谁在使用计算机,您必须连接到计算机并确定他们是否有本地配置文件,但即使这样也不完美,因为这些配置文件可能会被删除。