<p>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).</p>

<p>Is it possible?</p>

[{'answer_id': 52257, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 1, 'selected': False, 'text': '<p>You could loop the groups like i did in this answer:</p>\n\n<p><a href="https://stackoverflow.com/questions/45437/determining-members-of-local-groups-via-c#45458">Determining members of local groups via C#</a></p>\n\n<p>After reading some more, the easiest thing would be to use the <code>System.DirectoryServices.AccountManagement</code> namespace. Here is how it can be used:</p>\n\n<p><a href="http://www.leastprivilege.com/SystemDirectoryServicesAccountManagement.aspx" rel="nofollow noreferrer">http://www.leastprivilege.com/SystemDirectoryServicesAccountManagement.aspx</a></p>\n\n<p>Sample:</p>\n\n<pre><code>public static bool IsUserInGroup(string username, string groupname, ContextType type)\n{\n    PrincipalContext context = new PrincipalContext(type);\n\n    UserPrincipal user = UserPrincipal.FindByIdentity(\n        context,\n        IdentityType.SamAccountName,\n        username);\n    GroupPrincipal group = GroupPrincipal.FindByIdentity(\n        context, groupname);\n\n    return user.IsMemberOf(group);\n}\n</code></pre>\n'}, {'answer_id': 52265, 'author': 'Anders', 'author_id': 3501, 'author_profile': 'https://Stackoverflow.com/users/3501', 'pm_score': 3, 'selected': True, 'text': '<p>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</p>\n\n<p><strong>Edit:</strong> See <a href="http://www.pinvoke.net/default.aspx/advapi32/CheckTokenMembership.html" rel="nofollow noreferrer">pinvoke.net</a> for .NET sample code (Thanks chopeen)</p>\n'}, {'answer_id': 52267, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 2, 'selected': False, 'text': '<p>There is a Win32 API for this you could P/Invoke: <a href="http://msdn.microsoft.com/en-us/library/bb776463(VS.85).aspx" rel="nofollow noreferrer">IsUserAnAdmin</a></p>\n\n<p>The question is more complex on Vista ... see this <a href="http://blogs.msdn.com/cjacks/archive/2006/10/09/How-to-Determine-if-a-User-is-a-Member-of-the-Administrators-Group-with-UAC-Enabled-on-Windows-Vista.aspx" rel="nofollow noreferrer">blog post</a>.</p>\n'}, {'answer_id': 310125, 'author': 'Ryan', 'author_id': 20198, 'author_profile': 'https://Stackoverflow.com/users/20198', 'pm_score': 1, 'selected': False, 'text': '<p>If you are talking about the currently running user then</p>\n\n<pre><code>using System.Security.Principal;\n\nWindowsIdentity identity = WindowsIdentity.GetCurrent();\nWindowsPrincipal wp = new WindowsPrincipal(identity);\n\nif (wp.IsInRole("BUILTIN\\Administrators"))\n   // Is Administrator\nelse\n   // Is Not\n</code></pre>\n\n<p>If not then I expect its possible to set <strong>identity</strong> to a particular user but not looked into how.</p>\n'}]
['https://Stackoverflow.com/questions/52256', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/95/']