C# Return list of groups a user belongs to in a desktop application

I'm working with a C# Windows desktop application. I want to return the list of groups that the currently logged in user is a member of in windows. I'd like to display the list on a winform is a listbox. I'm returning the user, but I'm not sure how to return the groups they belong to. Thanks for any help.
dodgerfanAsked:
Who is Participating?
 
it_saigeDeveloperCommented:
If you need only security groups, then all you need to do is add a reference to System.DirectroyServices.AccountManagement and use the UserPrincipal.GetAuthorizationGroups() Method.  Example implementation -
using System;
using System.DirectoryServices.AccountManagement;

namespace EE_Q29093191
{
    class Program
    {
        static void Main(string[] args)
        {
            var user = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain, Environment.GetEnvironmentVariable("USERDOMAIN")), Environment.GetEnvironmentVariable("USERNAME"));
            if (user != null)
            {
                foreach (var role in user.GetAuthorizationGroups())
                {
                    Console.WriteLine(role);
                }
            }
            Console.ReadLine();
        }
    }
}

Open in new window


-saige-
0
 
Chinmay PatelEnterprise ArchitectCommented:
When you say Groups, you mean to say local groups or AD Groups?
0
 
dodgerfanAuthor Commented:
Sorry, AD groups,
0
 
Chinmay PatelEnterprise ArchitectCommented:
No need to say sorry :) I just wanted to make sure that we go in the right direction.

You can use this sample from MSDN and get all the groups for a given user - including recursive / nested groups.

https://code.msdn.microsoft.com/windowsdesktop/How-to-list-group-and-user-d8b0eb42
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.