Essentially what I'm trying to do is add a computer/user to a group. After I add the object to the group, I want to query the groups of the object to see what they have.
It seems that the GetGroups method doesn't update fast enough. My test always seems to fail. If I put some breakpoints in VS, it will run if I wait enough.
I'm new to playing with the AccounManagement namespace (I've used the pre .net 3.5 code alot). I guess I could loop through the code a few times but I'm seeing if other people have suggestions for this.
I've done the following unit test
[Test]
public void Check()
{
string distinguishedName = "ComputerDistinguishedName";
string groupDN = "GroupDistinguished name";
// Remove the identity from the group so it does crashes if it's already part of it.
GroupCtrl.RemoveIdentityFromGroup(groupDN, distinguishedName);
using (var ctx = new PrincipalContext(ContextType.Domain))
{
var group = GroupPrincipal.FindByIdentity(ctx, groupDN);
Console.WriteLine(group.Members.Count);
if (!group.Members.Contains(ctx, IdentityType.DistinguishedName, distinguishedName))
{
group.Members.Add(ctx, IdentityType.DistinguishedName, distinguishedName);
group.Save();
}
foreach (var item in group.Members)
{
Console.WriteLine(item.DistinguishedName);
}
Console.WriteLine(group.Members.Count);
}
var isMemberOf = false;
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
var found = Principal.FindByIdentity(ctx, IdentityType.DistinguishedName, distinguishedName);
if (found != null)
{
Console.WriteLine(found.DistinguishedName);
foreach (var item in found.GetGroups())
{
Console.WriteLine(item.DistinguishedName);
if (item.DistinguishedName == groupDN)
{
isMemberOf = true;
}
}
}
Assert.AreEqual(true, isMemberOf);
}
// Reset our group membership to run the test again.
GroupCtrl.RemoveIdentityFromGroup(groupDN, distinguishedName);
}
Edit 1:
So I tried two different approaches
1 -
I tried getting the getUnderlyingObject and then looping through the memberOf properties (same result)
2 -
I avoided the AccountManagement code and used a DirectorySearcher and looped through the memberOf property and it comes up every time. Sighh