Why would
GroupPrincipal group = GroupPrincipal.FindByIdentity(getPrincipalContext(),
"TEST_DESTINATION_GRP");
return null
? I know for a fact that TEST_DESTINATION_GRP
exists under the groups organizational unit.
My code:
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 Princ开发者_JAVA百科ipalContext getPrincipalContext()
{
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain,sDefaultOU);
return oPrincipalContext;
}
The output of the above code is:
Domain Users GRP_ADMIN_SERVER GRP_PROG_III CTXXA-FlexUser CTXXA-Users Wtf Abanico, Elnora@MSH empty why!!!!
Any thoughts?
What are the values in sDomain
and sDefaultOU
in your getPrincipalContext
method??
I would try the following steps:
Construct your
PrincipalContext
without any domain or OU name - in that case, your default domain and its top-level node will be used:PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
Search now - does it find your group you're looking for??
Check to make sure the domain and
sDefaultOU
values are correct - if you connect your principal context to one OU, you cannot search in another OU, obviuosly (unless that's a child-OU of the OU you're connecting to).
精彩评论