2

I need to check programmatically (in .NET) whether a given user (domain account) is a member of the built-in Administrators group on a current computer (the one where the application gets executed).

Is it possible?

Espo
  • 41,399
  • 21
  • 132
  • 159
Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111

4 Answers4

2

I don't know about .Net, but in win32, the easy way is to call IsUserAnAdmin(). If you need more control, you can open the process token and check with CheckTokenMembership for each group you need to check

Edit: See pinvoke.net for .NET sample code (Thanks chopeen)

Anders
  • 97,548
  • 12
  • 110
  • 164
2

There is a Win32 API for this you could P/Invoke: IsUserAnAdmin

The question is more complex on Vista ... see this blog post.

Rob Walker
  • 46,588
  • 15
  • 99
  • 136
  • "IsUserAnAdmin function is a wrapper for CheckTokenMembership. It is recommended to call that function directly to determine Administrator group status rather than calling IsUserAnAdmin". (From the linked page) – Espo Sep 09 '08 at 16:31
  • I hadn't noticed the warning before. I wonder why Microsoft would consider deprecating such a simpler helper ... fortunately they have a good track record for backwards compatibility. – Rob Walker Sep 09 '08 at 17:48
1

If you are talking about the currently running user then

using System.Security.Principal;

WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal wp = new WindowsPrincipal(identity);

if (wp.IsInRole("BUILTIN\Administrators"))
   // Is Administrator
else
   // Is Not

If not then I expect its possible to set identity to a particular user but not looked into how.

Ryan
  • 23,871
  • 24
  • 86
  • 132
1

You could loop the groups like i did in this answer:

Determining members of local groups via C#

After reading some more, the easiest thing would be to use the System.DirectoryServices.AccountManagement namespace. Here is how it can be used:

http://www.leastprivilege.com/SystemDirectoryServicesAccountManagement.aspx

Sample:

public static bool IsUserInGroup(string username, string groupname, ContextType type)
{
    PrincipalContext context = new PrincipalContext(type);

    UserPrincipal user = UserPrincipal.FindByIdentity(
        context,
        IdentityType.SamAccountName,
        username);
    GroupPrincipal group = GroupPrincipal.FindByIdentity(
        context, groupname);

    return user.IsMemberOf(group);
}
Community
  • 1
  • 1
Espo
  • 41,399
  • 21
  • 132
  • 159
  • The System.DirectoryServices.AccountManagement namespace is new to .NET 3.5, isn't it? – Marek Grzenkowicz Sep 10 '08 at 09:04
  • First sentence in the linked article: "Looking through some of the new 3.5 stuff I stumbled over a new assembly named "System.DirectoryServices.AccountManagement" - that caught my attention." – Espo Sep 11 '08 at 05:36
  • Sorry, I missed it. Could you add information CheckTokenMembership to your answer to make it complete (so I can mark it as accepted answer)? – Marek Grzenkowicz Sep 11 '08 at 09:46