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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
When you say Groups, you mean to say local groups or AD Groups?
0
dodgerfanAuthor Commented:
Sorry, AD groups,
0
Chinmay PatelChief Technical NinjaCommented:
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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.