title: How can I check if my program is really running as an administrator?
body: <p>We have a setup program that runs an MSBuild script which imports a self-signed certificate in the local computer account's Personal/My certificate store.  I am in the Administrators group on the server where setup is being run, and UAC is <em>not</em> enabled.  When I run my script, and import the certificate, I am unable to use it in IIS.  Process Monitor shows access denied errors when I try to assign it to a website.</p>
  
  <p>However, when I <strong>explicitly run my script as an administrator (right-click and choose "Run As Administrator"), the certificate is imported successfully</strong>, and I can use it in IIS.  This is extremely bizarre to me.</p>
  
  <p><strong>How can I tell if my script/program is running as an administrator?</strong>  I'd like to add a check to the setup script that fails if it detects it isn't running with this weird "Run As Administrator" privilege.  I would prefer an answer in C#/.NET.</p>
  
  <p>I've tried using <code>GetTokenInformation</code>, to get the elevation type, but that only works when UAC is enabled.</p>
  
  <p>Using <code>System.Security.Principal.WindowsIdentity.IsInRole(WindowsBuiltInRole.Administrator)</code> return true in a regular and "elevated" prompt.</p>
  
  <p>I've compared the Owner, User, and Group SIDS exposed by <code>System.Security.Principal.WindowsIdentity.GetCurrent</code>, and the list is the same in a regular and "elevated" prompt.</p>