What's the easiest way to get the primary group name from AD in C #?

I am currently using PrincipalContext and UserPrincipal to return the primary group of users.

How can I take this id and find the actual group name?

Also I have code that works correctly for assigning the primary group of a user, but once I assign them to a group, I cannot remove them from Domain Users, which is the default primary group until I changed. I called Save()

before trying to delete the domain user group.

My requirements: I have to add the user to AD and then assign my primary group and then delete them as a member of the domain users.

+2


a source to share


3 answers


Finally got



 PrincipalContext principalContext = this.principalFactory.CreateActiveDirectoryManagementContext(locationType);
        UserPrincipal userPrincipal = this.principalFactory.CreateUserPrincipal(principalContext, userName);

        string primaryGroupId = userPrincipal.GetPrimaryGroupId();

        PrincipalSearchResult<Principal> results =
            userPrincipal.GetAuthorizationGroups();

        foreach (Principal principal in from principal in results
                                        let sid = principal.Sid.ToString()
                                        let test = sid.Split('-').ToList()
                                        let count = test.Count
                                        where test[count - 1].Equals(primaryGroupId)
                                        select principal)
        {
            return principal.Name;
        }

        return string.Empty;

      

+2


a source


Without seeing your code, it's hard to know for sure, but it looks like you're almost there! I had a similar problem a few years ago and this blog post was very helpful to me. This Scripting Guy article goes into more detail about the steps.

I don't know if you can do this with the System.DirectoryServices.AccountManagement stuff. Microsoft has simplified some common AD tasks with this namespace, but I'd be surprised if this was one of them.

With regard to deleting the Domain Users group, this is not possible until the primary group is changed.

This is untested pseudocode, but I think something like this will work.



// get the group
DirectoryEntry groupToAdd = new DirectoryEntry("LDAP://" + groupDistinguishedName);
// add the member
groupToAdd.Properties["member"].Add(userDistinguishedName);
// commit and close
groupToAdd.CommitChanges();
groupToAdd.Close();

      

You said that you already know how to assign a primary group, so once you've done that and you're done, you can remove the Domain Users membership.

//Get the domain users
DirectoryEntry domainUsers = new DirectoryEntry("LDAP://" + domainUserDistinguishedName);
// Remove the user from the domain user group
domainUsers.Properties["member"].Remove(userDistinguishedName);
//Commit the changes
domainUsers.CommitChanges();
domainUsers.Close();

      

For reference, here's a good AD in C # overview . Hope this helps!

+1


a source


Also, if PowerShell is an option, it looks like it will do almost what you want.

0


a source







All Articles