Get User is Member of - Windows C# RRS feed

  • Question

  • Hello,

    How can I find out User is of which group means user is member of ? I am fetching user name from textbox and on button click I want the list of groups to which Textbox user belongs in windows not in Active directory server. I am having windows professional OS.

    Wednesday, September 26, 2018 11:59 AM

All replies

  • Take a look at

    If the user name you are using is not for the current user then you will need to construct a WindowsIdentity object for that user.

    • Edited by RLWA32 Wednesday, September 26, 2018 2:08 PM
    Wednesday, September 26, 2018 2:06 PM
  • In my case user is not the current user, can you please show me an example of creating WindowsIdentity object for non current user.

    Wednesday, September 26, 2018 2:43 PM
  • Have you looked at the various WindowsIdentity constructors available?
    Wednesday, September 26, 2018 2:57 PM
  • Another option is to use P/Invoke to call NetUserGetLocalGroups
    Wednesday, September 26, 2018 4:19 PM
  • I have tried and found that using advapi32.dll and LogobUser Function I can get the token of the user and based on that token I can pass the same to WindowsIdentity to fetch groups.

    But I my case I am getting each and every group like NT Authority etc I want only those groups which are listed in user properties in membership tab.

    Thursday, September 27, 2018 5:39 AM
  • Hi Tech Aspirant,

    Please try the following the code, which works on my side. 

    please add the following reference first.

    using System.Management;

    SelectQuery query = new SelectQuery("Win32_UserAccount");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    foreach (ManagementObject envVar in searcher.Get())
         Console.WriteLine("Username : {0}", envVar["Name"]);

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, September 27, 2018 7:52 AM