I want to add a windows account user to a group
I use this method:
public bool AddUserToGroup(PrincipalContext ctx, string userId, string groupName)
{
try
{
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, groupName);
group.Members.Add(ctx, IdentityType.UserPrincipalName, userId);
group.Save();
return true;
}
catch
{
return false;
}
}
when I use this method with the following
PrincipalContext
new PrincipalContext(ContextType.Domain, "Lab.net");
It work Fine.
but when i use
PrincipalContext
with username and password it have exception
new PrincipalContext(ContextType.Domain, "Lab.net","administrator","P@ssw0rd");
the exception is:
System.DirectoryServices.AccountManagement.PrincipalOperationException: Information about the domain could not be retrieved (1355).
at System.DirectoryServices.AccountManagement.Utils.GetDcName(String computerName, String domainName, String siteName, Int32 flags)
at System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo()
at System.DirectoryServices.AccountManagement.ADStoreCtx.get_DnsForestName()
at System.DirectoryServices.AccountManagement.ADUtils.ArePrincipalsInSameForest(Principal p1, Principal p2)
at System.DirectoryServices.AccountManagement.ADStoreCtx.UpdateGroupMembership(Principal group, DirectoryEntry de, NetCred credentials, AuthenticationTypes authTypes)
at System.DirectoryServices.AccountManagement.SDSUtils.ApplyChangesToDirectory(Principal p, StoreCtx storeCtx, GroupMembershipUpdater updateGroupMembership, NetCred credentials, AuthenticationTypes authTypes)
at System.DirectoryServices.AccountManagement.ADStoreCtx.Update(Principal p)
at System.DirectoryServices.AccountManagement.Principal.Save()
I Can create user with this
PrincipalContext
but i cant join a user to a group
What I have tried:
How to add a user account to a group (member of)