Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can work out if a local account exists through the <a href="https://msdn.microsoft.com/en-us/library/system.security.principal.aspx" rel="nofollow noreferrer"><code>System.Security.Principal</code></a> namespace using the following code. </p> <pre><code>bool AccountExists(string name) { bool bRet = false; try { NTAccount acct = new NTAccount(name); SecurityIdentifier id = (SecurityIdentifier)acct.Translate(typeof(SecurityIdentifier)); bRet = id.IsAccountSid(); } catch (IdentityNotMappedException) { /* Invalid user account */ } return bRet; } </code></pre> <p>Now getting group membership is slightly harder, you can easily do it for the current user using the <a href="https://msdn.microsoft.com/en-us/library/system.security.principal.windowsprincipal.isinrole.aspx" rel="nofollow noreferrer"><code>WindowsPrinciple.IsInRole</code></a> method (creating a principle from the <a href="https://msdn.microsoft.com/en-us/library/sfs49sw0.aspx" rel="nofollow noreferrer"><code>WindowsIdentify.GetCurrent()</code></a> method). </p> <p>As pointed out I don't think there is a way of getting anything else without resorting to PInvoke or WMI. So here is a bit of code to check group membership with WMI.</p> <pre><code>bool IsUserInGroup(string name, string group) { bool bRet = false; ObjectQuery query = new ObjectQuery(String.Format("SELECT * FROM Win32_UserAccount WHERE Name='{0}' AND LocalAccount=True", name)); ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); ManagementObjectCollection objs = searcher.Get(); foreach (ManagementObject o in objs) { ManagementObjectCollection coll = o.GetRelated("Win32_Group"); foreach (ManagementObject g in coll) { bool local = (bool)g["LocalAccount"]; string groupName = (string)g["Name"]; if (local &amp;&amp; groupName.Equals(group, StringComparison.InvariantCultureIgnoreCase)) { bRet = true; break; } } } return bRet; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload