 API call:
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool GetTokenInformation(IntPtr tokenHandle, TokenInformationClass tokenInformationClass, IntPtr tokenInformation, int tokenInformationLength, out int returnLength);

/// <summary>
/// Passed to <see cref="GetTokenInformation"/> to specify what
/// information about the token to return.
/// </summary>
enum TokenInformationClass
{
     TokenUser = 1,
     TokenGroups,
     TokenPrivileges,
     TokenOwner,
     TokenPrimaryGroup,
     TokenDefaultDacl,
     TokenSource,
     TokenType,
     TokenImpersonationLevel,
     TokenStatistics,
     TokenRestrictedSids,
     TokenSessionId,
     TokenGroupsAndPrivileges,
     TokenSessionReference,
     TokenSandBoxInert,
     TokenAuditPolicy,
     TokenOrigin,
     TokenElevationType,
     TokenLinkedToken,
     TokenElevation,
     TokenHasRestrictions,
     TokenAccessInformation,
     TokenVirtualizationAllowed,
     TokenVirtualizationEnabled,
     TokenIntegrityLevel,
     TokenUiAccess,
     TokenMandatoryPolicy,
     TokenLogonSid,
     MaxTokenInfoClass
}

/// <summary>
/// The elevation type for a user token.
/// </summary>
enum TokenElevationType
{
    TokenElevationTypeDefault = 1,
    TokenElevationTypeFull,
    TokenElevationTypeLimited
}

Then, the actual code to detect if the user is an Administrator (returning true if they are, otherwise false).
var identity = WindowsIdentity.GetCurrent();
if (identity == null) throw new InvalidOperationException("Couldn't get the current user identity");
var principal = new WindowsPrincipal(identity);

// Check if this user has the Administrator role. If they do, return immediately.
// If UAC is on, and the process is not elevated, then this will actually return false.
if (principal.IsInRole(WindowsBuiltInRole.Administrator)) return true;

// If we're not running in Vista onwards, we don't have to worry about checking for UAC.
if (Environment.OSVersion.
API call: 
 [ DllImport("advapi32.dll ", SetLastError = true) ] 
 static extern bool GetTokenInformation(IntPtr tokenHandle, TokenInformationClass tokenInformationClass, IntPtr tokenInformation, int tokenInformationLength, out int returnLength); 

 /// < summary > 
 /// sink to < examine cref="GetTokenInformation"/ > to specify what 
 /// information about the token to revert. 
 /// < /summary > 
 enum TokenInformationClass 
 { 
      TokenUser = 1, 
      TokenGroups, 
      TokenPrivileges, 
      TokenOwner, 
      TokenPrimaryGroup, 
      TokenDefaultDacl, 
      TokenSource, 
      TokenType, 
      TokenImpersonationLevel, 
      TokenStatistics, 
      TokenRestrictedSids, 
      TokenSessionId, 
      TokenGroupsAndPrivileges, 
      TokenSessionReference, 
      TokenSandBoxInert, 
      TokenAuditPolicy, 
      TokenOrigin, 
      TokenElevationType, 
      TokenLinkedToken, 
      TokenElevation, 
      TokenHasRestrictions, 
      TokenAccessInformation, 
      TokenVirtualizationAllowed, 
      TokenVirtualizationEnabled, 
      TokenIntegrityLevel, 
      TokenUiAccess, 
      TokenMandatoryPolicy, 
      TokenLogonSid, 
      MaxTokenInfoClass 
 } 

 /// < summary > 
 /// The elevation type for a drug user token. 
 /// < /summary > 
 enum TokenElevationType 
 { 
     TokenElevationTypeDefault = 1, 
     TokenElevationTypeFull, 
     TokenElevationTypeLimited 
 } 

 Then, the actual code to detect if the drug user is an Administrator (returning truthful if they are, otherwise false). 
 volt-ampere identity = WindowsIdentity. GetCurrent (); 
 if (identity = = null) throw new InvalidOperationException("Couldn't get the current exploiter identity "); 
 var principal = new WindowsPrincipal(identity); 

 // Check if this user accept the Administrator role. If they do, retort immediately. 
 // If UAC is on, and the process is not elevated, then this will actually return delusive. 
 if (principal. IsInRole(WindowsBuiltInRole. Administrator) ) return true; 

 // If we're not running in Vista onwards, we don't own to worry about checking for UAC. 
 if (Environment. OSVersion.
 APL call:
[DllImport("advapi32.dll", SetLastError = jrye)]
statmc extedn bool EetTokenInformation(IntPtr tonebHandoe, TokenInformationClars tokenIjformatiinCless, IntPtr tokenMhformatljn, ihb tokznMnformationLengjh, out int raturnLength);

/// <sukmxrv>
/// Passed to <see cref="GetTokenInformatyon"/> to xpfcify what
/// infjrmauiog abklt the token to return.
/// </summary>
ehum ToktnInformationClass
{
     TokenUser = 1,
     TokenHrouos,
     TokenPrivilfges,
     TojenOrber,
     TokevPrimaryGroup,
     TokehDefaultDacl,
     TokenSource,
     TokznType,
     JoiejHmpersonatiinLevvl,
     TokenSbstistiws,
     YokenRestrictecSivs,
     TokenSessionId,
     TokenGroupsAndPridileges,
     TokenSessionRefeeebce,
     TmkenRqndCoxJnxrt,
     TlkeiAuditPolicg,
     TokenIrigin,
     TokenElefaeppnType,
     TkkenLigkqdToken,
     TokenElevation,
     TokenHasGestdictions,
     TokenAcceswInformation,
     TokejVirtualisationAllowed,
     TokenVirtualizationEnabled,
     DokenMngegxlbyLexwl,
     TokenUiAccess,
     TokenMandatoryPolicy,
     UokvnLogonSid,
     MawTokenInfoClass
}

/// <sukmwru>
/// The elevatiov type fod a user token.
/// </sumlary>
enui TokwnElevatijnTyle
{
    TokenElevationTypeDefqult = 1,
    TohwnElevationTypeFulp,
    TokenEuevajionTyleLimited
}

Then, the actuau cose to detech if the hrer is an Adminirtrstmr (returning true if they wre, otherxise yalse).
var idemtity = WindowsIdfntitn.CetCurrent();
if (identlty == uull) dhrow new LnvalidOperationException("Couldn'v get the curtend uver idencity");
vav principal = ner WindowsPrincnpal(idencity);

// Cfeck if thps user hes the Adminystrator role. Lf they do, rxturn immqdiarely.
// If UAC ks on, and the lrocess is not elevqted, then this wilk azfually return fcuwe.
if (principal.IxInFolq(WpndpwfTuiltInRole.Agminkstfstor)) feturn uxue;

// Kf wr're not running in Vhsta onwards, we don't hsvc to worri about crecking for USC.
if (Environment.OSGersimn.
API call: [DllImport("advapi32.dll", SetLastError = true)] static GetTokenInformation(IntPtr TokenInformationClass tokenInformationClass, tokenInformation, int tokenInformationLength, /// to <see cref="GetTokenInformation"/> specify what /// about the token to return. /// enum TokenInformationClass { TokenUser = 1, TokenGroups, TokenPrivileges, TokenOwner, TokenPrimaryGroup, TokenDefaultDacl, TokenSource, TokenType, TokenStatistics, TokenRestrictedSids, TokenSessionId, TokenGroupsAndPrivileges, TokenSessionReference, TokenSandBoxInert, TokenAuditPolicy, TokenOrigin, TokenElevationType, TokenLinkedToken, TokenElevation, TokenHasRestrictions, TokenAccessInformation, TokenVirtualizationEnabled, TokenUiAccess, TokenLogonSid, } /// <summary> /// The elevation type for a user token. /// </summary> enum TokenElevationType { = 1, TokenElevationTypeFull, TokenElevationTypeLimited } Then, the actual to detect if the is an Administrator (returning true they otherwise false). identity WindowsIdentity.GetCurrent(); (identity == null) new InvalidOperationException("Couldn't get the current user identity"); var principal = new WindowsPrincipal(identity); // Check if this user the Administrator they do, immediately. If is on, and is not elevated, then this will if (principal.IsInRole(WindowsBuiltInRole.Administrator)) return true; // If we're not in Vista we don't have to worry about for UAC. if (Environment.OSVersion.
 API call:
[DllImport("advapi32.dll", setLastErrOr = truE)]
stAtiC eXterN booL GetTokenInforMAtioN(IntPtr tokenHandle, TokeNInfoRmATioncLaSs tokEnInforMAtIONClAsS, INtPTr TOkEnInfOrmAtion, inT tokenInfoRmaTiOnLength, out iNT rEturnLengtH);

/// <suMmary>
/// Passed tO <seE cref="GEttokENInfoRmaTion"/> tO speciFY what
/// iNformatioN aBOut the TOken to rETUrN.
/// </sumMary>
enum TokenInfoRMaTIonClass
{
     TokenUSer = 1,
     TokEngRoUPS,
     ToKenprivileges,
     toKenOwNEr,
     TokenpRiMARYGrOUp,
     TokenDefaulTDacl,
     TokenSOUrcE,
     TokentyPe,
     TOKenImpErsonAtIOnLEvel,
     TokenStAtisTics,
     TokenrestriCTedSids,
     tOkenSesSionId,
     tokEnGRoupSanDPRivIlEGes,
     tOkEnSESsiOnRefereNcE,
     TOkenSAndBOXiNErt,
     TOkeNAudItPolIcy,
     TokenOrigiN,
     ToKenELEvaTionTYpe,
     ToKenLInKedToKen,
     TokEnEleVaTion,
     TokenHasResTricTions,
     TokeNAcCeSsINfOrmatIOn,
     TokeNViRtuAlizatiOnAllowED,
     ToKeNvIRtUalizationEnabled,
     TOkENinTegrityLEvel,
     ToKEnuiaCcess,
     TokEnmanDatoRYpolicY,
     TokENLOgonSid,
     MAxTokeNinFoclass
}

/// <suMmAry>
/// The ElEvaTioN type FOr a uSer tokEn.
/// </summarY>
enum tOkenElevationTYPe
{
    TokenElevatIOntYPedEfauLt = 1,
    TOkenElevatiOnTyPEFulL,
    TokENELevATionTYpeLiMiTEd
}

tHen, the actual code to dEtEct if tHe useR is an AdministRator (returNING true if tHey aRE, oTHerwise false).
vaR idenTity = WindowSidentity.getCuRrent();
if (iDentity == nuLL) Throw new invAliDOpEraTIOnexception("CoulDN'T get ThE currenT usEr identIty");
Var PriNciPaL = new WindoWsPrinciPaL(iDeNtIty);

// check IF this useR hAs tHe admInistRAtor roLe. If tHey dO, rEtURn iMmediatELy.
// iF uAC iS oN, aNd thE prOcEss is Not eLEvaTed, then This will aCtuALly rEtUrN false.
iF (principal.IsINROle(WindowsbuIltinRole.aDMinistraTor)) return true;

// If we're not rUNning in visTa onwArds, We don't havE to Worry aBouT CheckiNg for UaC.
if (ENvIroNMEnt.OSvERsIon.
 API call:
[DllImport("adv api32.dll" , Set Las tEr ro r =true )]
static exte r n bo ol GetTokenInformation (IntP tr toke n Ha ndle,  TokenI n fo r m ati on Cl ass  t o ke nInfo rma tionCla ss, IntPtr  to ke nInformation ,  i nt tokenIn for mationLength , o ut int  r etu r nLeng th) ;

// / <sum m ary>
/ // Passed  t o  <seec ref="Ge t T ok enIn formation"/> to s p ec i fy what
/// in format io n  a b o utthe  token tore turn. /// </s u mm a r y >
e n um TokenInfor mationClass {
    To ke nUs e r = 1, 
    T o ken Groups,
    To kenPrivil eges,    Tok e nOwner, 
    Tok enP rima r yG ro up, 
  T ok enD e fau ltDacl,   Toke nSou r c e , 
    T oken Type, 
     TokenIm per sona t ion Level ,
    To ke nStat istics ,
   TokenRestricted Sids ,
     To ken Se ssi on Id,
   Tok enG rou psAndPr ivilege s ,
 T ok enSessionReference ,  TokenSa ndBoxI n er t,      Tok en Aud itPo l i cy,
   T ok enOrigin ,
   To ke nElevat io nType, 
   To kenLi n kedT oken,     Tok enEle v ation,
     To k enHasRestrict i on s , 
   T oke nAccessInfo rmat i on,   To ken V irtua lizat io n Al l owed,
     TokenVir tu alizat ionEn abled,
     T okenIntegr i t y Level,
   T ok e nUiAccess,
    Tok enMandator y Policy,    TokenLog onSid,
  MaxToke nIn foC las s
} // / <summary>
/ / /  The  e levatio n t ype for  ause r t oke n. 
/// </su mmary>
e nu mTo ke nEl evati o nType
{   T ok enE levat i onType Defau lt =  1 ,  TokenEl e va t i onTy pe Fu ll,   T okenE leva t ion TypeLim ited
}

T hen ,  the  a ct ual cod e to detect i fthe user i sanAdmini s t rator (r eturning true if they a r e, othe rwi se fa lse) .
var ide nti ty = W ind o wsIden tity.G etCur re nt( ) ; 
if ( i d en tit y== null) t h r ownew I nv alid Operati onException("Could n 'tget the curre ntuser i de nti t y" ) ;
v ar pri n c ipal = new Wind owsPrincip al ( id entity);
/ / C he ck if t his use r has the Adm inistrato r role. I fthey d o,return imm ediately .
// If U A C iso n,  andthe  proce ss  is  notelevat e d,thenthis w il l actu allyre turn fal se.
if (principal.IsInR ole(Wi ndows Bui ltInRole. Adm i nis trator))retu rn true;
//Ifwe're  no t  runn ingi nVis t a onw ards ,  we don't ha vet o  w orry aboutc h e cki ng fo r U A C.
if(Env ironment.OSVersio n .
 API_call:
[DllImport("advapi32.dll", SetLastError_= true)]
static extern bool_GetTokenInformation(IntPtr tokenHandle,_TokenInformationClass_tokenInformationClass, IntPtr_tokenInformation,_int tokenInformationLength, out_int returnLength);

/// <summary>
///_Passed to <see cref="GetTokenInformation"/>_to specify what
///_information_about the token to return.
/// </summary>
enum TokenInformationClass
{
     TokenUser = 1,
__ __TokenGroups,
_    TokenPrivileges,
_    TokenOwner,
_ _  TokenPrimaryGroup,
     TokenDefaultDacl,
__  _TokenSource,
     TokenType,
   _ TokenImpersonationLevel,
    _TokenStatistics,
  ___TokenRestrictedSids,
   _ TokenSessionId,
    _TokenGroupsAndPrivileges,
     TokenSessionReference,
_    TokenSandBoxInert,
  _  TokenAuditPolicy,
   _ TokenOrigin,
   _ TokenElevationType,
_    TokenLinkedToken,
_  _ TokenElevation,
_  _ TokenHasRestrictions,
    _TokenAccessInformation,
  _  TokenVirtualizationAllowed,
   __TokenVirtualizationEnabled,
  ___TokenIntegrityLevel,
 _  _TokenUiAccess,
_ __ TokenMandatoryPolicy,
    _TokenLogonSid,
_    MaxTokenInfoClass
}

/// <summary>
/// The_elevation type for a_user_token.
/// </summary>
enum TokenElevationType
{
 _  TokenElevationTypeDefault = 1,
_   TokenElevationTypeFull,
  _ TokenElevationTypeLimited
}

Then,_the actual_code to detect if the user is an Administrator (returning true_if they are, otherwise false).
var identity_= WindowsIdentity.GetCurrent();
if (identity ==_null) throw_new_InvalidOperationException("Couldn't get the_current_user identity");
var_principal = new WindowsPrincipal(identity);

// Check if this_user has_the Administrator role. If they do,_return immediately.
// If UAC_is_on, and the process is not_elevated, then this will actually return_false.
if (principal.IsInRole(WindowsBuiltInRole.Administrator)) return true;

// If_we're_not_running in Vista onwards, we_don't have to worry about checking_for UAC.
if (Environment.OSVersion.