2
爲什麼會爲什麼GroupPrincipal.FindByIdentity即使在我知道該組存在時也會返回null?
GroupPrincipal group = GroupPrincipal.FindByIdentity(getPrincipalContext(),
"TEST_DESTINATION_GRP");
回報null
?我知道TEST_DESTINATION_GRP
存在於組織單位下。
我的代碼:
private void addUserToGroup(string userName, string groupName)
{
try
{
UserPrincipal user = UserPrincipal.FindByIdentity(getPrincipalContext(), IdentityType.SamAccountName, "jcolon");
GroupPrincipal group = GroupPrincipal.FindByIdentity(getPrincipalContext(), "TEST_DESTINATION_GRP");
//just to show that I can access AD
ArrayList x = getUserGroups(userName);
foreach (var xy in x)
{
Console.WriteLine(xy);
}//I can access AD FINE
if (group == null)
{ Console.WriteLine("Wtf!"); }
Console.WriteLine(user);
Console.WriteLine(group + "empty why!!!!");
}
catch (Exception e)
{
//log e
}
}
private PrincipalContext getPrincipalContext()
{
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain,sDefaultOU);
return oPrincipalContext;
}
上述代碼的輸出是:
Domain Users GRP_ADMIN_SERVER GRP_PROG_III CTXXA-FlexUser CTXXA-Users Wtf Abanico, [email protected] empty why!!!!
有什麼想法?