system.DirectoryServices.AccountManagement中的GetGroups方法似乎会刷新
本文关键字:刷新 方法 GetGroups DirectoryServices AccountManagement 中的 system | 更新日期: 2023-09-27 18:15:53
实际上我要做的是将计算机/用户添加到组中。将对象添加到组之后,我想查询对象所属的组,看看它们有什么。
似乎GetGroups方法更新得不够快。我的考试似乎总是不及格。如果我在VS中设置了一些断点,如果我等待足够长的时间,它就会运行。
我刚开始使用AccounManagement命名空间(我已经使用了很多。net 3.5之前的代码)。我想我可以在代码中循环几次,但我想看看其他人是否对此有建议。
我已经完成了以下单元测试
[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);
}
编辑1:
所以我尝试了两种不同的方法
<标题> 1 - h1> 尝试获得getUnderlyingObject,然后循环通过memberOf属性(相同的结果) <标题> 2 - h1> 避免了AccountManagement代码,使用了DirectorySearcher并循环遍历memberOf属性,并且每次都会出现。Sighh 标题>标题>因此我将代码更改为以下内容。用DirectorySearch检查memberOf的旧方法每次都有效。我希望在这个项目中只使用AccountManagement类。我想知道这个类的未来版本是否会更好。
[Test]
public void Check()
{
//var test = new Constructor();
var test = new AdContextObject();
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 = test.GetContext())
{
var group = GroupPrincipal.FindByIdentity(ctx, groupDN);
Console.WriteLine(group.Members.Count);
if (!group.Members.Contains(ctx, IdentityType.DistinguishedName, distinguishedName))
{
Console.WriteLine("addGroup");
group.Members.Add(ctx, IdentityType.DistinguishedName, distinguishedName);
group.Save();
}
foreach (var item in group.Members)
{
Console.WriteLine(item.DistinguishedName);
}
Console.WriteLine(group.Members.Count);
}
DirectoryEntry de = new DirectoryEntry();
de.Path = "LdapSource";
DirectorySearcher ser = new DirectorySearcher(de);
ser.Filter = "(&(ObjectCategory=computer)(name=ComputerName))";
ser.PropertiesToLoad.Add("name");
ser.PropertiesToLoad.Add("memberOf");
var returnValue = ser.FindAll();
var isMemberOf = false;
foreach (SearchResult res in returnValue)
{
var memberOf = GetMultiValue(res, "MemberOf");
foreach (var item in memberOf)
{
Console.WriteLine(item);
if (item.Equals(groupDN, StringComparison.OrdinalIgnoreCase))
{
isMemberOf = true;
}
}
}
Assert.AreEqual(true, isMemberOf);
Console.WriteLine("old way worked fine");
isMemberOf = false;
using (PrincipalContext ctx = test.GetContext())
{
var found = Principal.FindByIdentity(ctx, IdentityType.DistinguishedName, distinguishedName);
if (found != null)
{
foreach (var item in found.GetGroups())
{
Console.WriteLine(item.DistinguishedName);
if (item.DistinguishedName.Equals(groupDN, StringComparison.OrdinalIgnoreCase))
{
isMemberOf = true;
}
}
}
Assert.AreEqual(true, isMemberOf);
}
// Reset our group membership to run the test again.
GroupCtrl.RemoveIdentityFromGroup(groupDN, distinguishedName);
}
public static string[] GetMultiValue(SearchResult result, string fieldName)
{
string[] returnValue = null;
if (result != null)
{
if (result.Properties.Contains(fieldName))
{
ResultPropertyValueCollection propertyValue = result.Properties[fieldName];
if (propertyValue != null)
{
if (propertyValue.Count > 1)
{
string[] valueArray = new string[propertyValue.Count];
for (int i = 0; i < propertyValue.Count; i++)
{
string valStr = propertyValue[i].ToString();
valueArray[i] = valStr;
}
returnValue = valueArray;
}
else if (propertyValue.Count == 1)
{
string[] tempString = new string[] { propertyValue[0].ToString() };
returnValue = tempString;
}
else
{
string[] tempString = new string[] { };
returnValue = tempString;
}
}
}
}
return returnValue;
}
public class AdContextObject
{
public PrincipalContext GetContext()
{
return new PrincipalContext(ContextType.Domain, "domainStuff", "MoreDomainStuff");
}
}