Search code examples
windows-authenticationusergroups

windows server 2016 identity group


I opened active directory users and computers, selected builtin and I created a group called Employee, and added my own current user to it. However when checking my groups in code like this I cannot see the Employee group

        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        var myPrincipal = new WindowsPrincipal(identity);
        identity.Groups.Select(x => "NTAccounts - " + x.Translate(typeof(NTAccount)).ToString()

`

and this is all I get, am I doing something wrong ? all I need is to have a few groups with users I can work with..

enter image description here

appreciate your advice


Solution

  • this code pulls information from active directory which is what I needed I just expected it to be at windows identity level, so my bad... resolved

    using (var ctx = new PrincipalContext(ContextType.Domain, "your DC name"))
                {
                    var myDomainUsers = new List<string>();
                    var userPrinciple = new UserPrincipal(ctx);
                    using (var search = new PrincipalSearcher(userPrinciple))
                    {
                        foreach (var domainUser in search.FindAll())
                        {
                            if (domainUser.DisplayName != null)
                            {
                                groupNames.Add("domainUser displayName - " + domainUser.DisplayName);
                                groupNames.Add("domainUser getGroups - " + domainUser.GetGroups().Select(x => x.Name).ToList().Join(","));
                            }
                        }
                    }
                }