getGroups system.directoryServices의 메소드 .AccountManagement가 새로 고침하는 것 같습니다
-
21-12-2019 - |
문제
본질적으로 내가하려는 것은 컴퓨터 / 사용자를 그룹에 추가하는 것입니다.그룹에 개체를 추가 한 후에는 객체 그룹을 쿼리하여 자신이 가진 것을 볼 수 있습니다.
getGroups 메소드가 충분히 빠르게 업데이트되지 않는 것 같습니다.내 테스트는 항상 실패한 것 같습니다.VS에 중단 점을 넣으면 충분히 기다리면 실행됩니다.
accounmanagement 네임 스페이스로 재생하는 것이 좋습니다 (Pre .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 -
GetUnderLyingObject를 얻은 다음 MemberOF 속성 (동일한 결과)을 통해 루핑을 시도했습니다.
2 -
i 계정 관리 코드를 피하고 DirectorySearcher를 사용하고 MermerOF 속성을 통해 루프로 묶었으며 매번 나타납니다.sighh
해결책
코드를 다음과 같이 변경했습니다.DirectorySearch가있는 MAMEROF를 사용하는 오래된 방법은 매번 작동합니다.이 프로젝트의 계정 관리 클래스 만 사용하기를 희망했습니다.나는 미래의 클래스 버전이 더 좋을지 궁금합니다.
[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");
}
}
. 제휴하지 않습니다 StackOverflow