Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add a windows account user to a group
I use this method:
C#
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
C#
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)
Posted
Updated 31-Jan-19 4:06am
v2

Have a look at the How to almost everything in active directory article. It helped me when I needed to work with the active directory.
 
Share this answer
 
Comments
Maciej Los 16-Jan-19 7:49am    
5ed!
Dylvh 18-Jan-19 1:34am    
Thank you
I change my method
It work
public bool AddUserToGroup(PrincipalContext ctx, DirectoryEntry userId, string groupName)
        {
            try
            {
                GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(ctx, groupName);
                if (groupPrincipal != null) {
                    DirectoryEntry entry = (DirectoryEntry)groupPrincipal.GetUnderlyingObject();
                    entry.Invoke("Add", new object[] { userId.Path.ToString() });
                    userId.CommitChanges();
                }
                else {
                    return true;
                }
                
                return true;
            }
            catch
            {
                return false;
            }
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900