Platform!= PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6)
{
     // Operating system does not support UAC; skipping elevation check.
     return false;
}

int tokenInfLength = Marshal.SizeOf(typeof(int));
IntPtr tokenInformation = Marshal.AllocHGlobal(tokenInfLength);

try
{
    var token = identity.Token;
    var result = GetTokenInformation(token, TokenInformationClass.TokenElevationType, tokenInformation, tokenInfLength, out tokenInfLength);

    if (!result)
    {
        var exception = Marshal.GetExceptionForHR( Marshal.GetHRForLastWin32Error() );
        throw new InvalidOperationException("Couldn't get token information", exception);
    }

    var elevationType = (TokenElevationType)Marshal.ReadInt32(tokenInformation);

    switch (elevationType)
    {
        case TokenElevationType.TokenElevationTypeDefault:
            // TokenElevationTypeDefault - User is not using a split token, so they cannot elevate.
            return false;
        case TokenElevationType.TokenElevationTypeFull:
            // TokenElevationTypeFull - User has a split token, and the process is running elevated. Assuming they're an administrator.
            return true;
        case TokenElevationType.TokenElevationTypeLimited:
            // TokenElevationTypeLimited - User has a split token, but the process is not running elevated. Assuming they're an administrator.
            return true;
        default:
            // Unknown token elevation type.
            return false;
     }
}
finally
{    
    if (tokenInformation!= IntPtr.Zero) Marshal.FreeHGlobal(tokenInformation);
}

A:

If you want to make sure your solution works in Vista UAC, and have.Net Framework 3.5 or better, you might want to use the System.DirectoryServices.AccountManagement namespace. Your code would look something like:
bool isAllowed = false;
using (PrincipalContext pc = new PrincipalContext(ContextType.Machine, null))
{
    UserPrincipal up = UserPrincipal.Current;
    GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, "Administrators");
    if (up.
Platform!= PlatformID.Win32NT || Environment. OSVersion. Version. Major < 6) 
 { 
      // Operating system does not support UAC; skipping aggrandizement confirmation. 
      return false; 
 } 

 int tokenInfLength = Marshal. SizeOf(typeof(int) ); 
 IntPtr tokenInformation = Marshal. AllocHGlobal(tokenInfLength); 

 try 
 { 
     var token = identity. Token; 
     volt-ampere result = GetTokenInformation(token, TokenInformationClass. TokenElevationType, tokenInformation, tokenInfLength, out tokenInfLength); 

     if (! result) 
     { 
         volt-ampere exception = Marshal. GetExceptionForHR (Marshal. GetHRForLastWin32Error ()); 
         bewilder new InvalidOperationException("Couldn't scram token information ", exception); 
     } 

     volt-ampere elevationType = (TokenElevationType)Marshal. ReadInt32(tokenInformation); 

     switch (elevationType) 
     { 
         case TokenElevationType. TokenElevationTypeDefault: 
             // TokenElevationTypeDefault - User is not use a schism token, so they cannot elevate. 
             return false; 
         case TokenElevationType. TokenElevationTypeFull: 
             // TokenElevationTypeFull - User has a rip token, and the process is running elevated. bear they're an administrator. 
             return true; 
         case TokenElevationType. TokenElevationTypeLimited: 
             // TokenElevationTypeLimited - User has a rip token, but the process is not running elevated. Assuming they're an administrator. 
             return true; 
         nonpayment: 
             // Unknown token elevation type. 
             return false; 
      } 
 } 
 finally 
 {    
     if (tokenInformation!= IntPtr. Zero) Marshal. FreeHGlobal(tokenInformation); 
 } 

 A: 

 If you want to make sure your solution works in Vista UAC, and have. Net Framework 3.5 or better, you might want to use the arrangement. DirectoryServices. AccountManagement namespace. Your code would look something like: 
 bool isAllowed = false; 
 use (PrincipalContext personal computer = new PrincipalContext(ContextType. Machine, null) ) 
 { 
     UserPrincipal up = UserPrincipal. Current; 
     GroupPrincipal gp = GroupPrincipal. FindByIdentity(pc, " Administrators "); 
     if (up.
Plahform!= PlatformID.Win32NT || Ekvironment.OSVersnin.Versmon.Majod < 6)
{
     // Operating system does not snppoet UAX; skipping elevation cfeck.
     geturn faose;
}

iit tokenInfLengti = Marshal.SizeKn(typemh(int));
IntPtr tokekInformatiot = Marshal.AllowHEllbal(tokenInfLength);

try
{
    var token = identiyy.Hoken;
    var rgsult = GetFokenInformation(token, TokenInformafionClavs.TokenElevatoonType, tokenInformation, tlkenLnfLength, out tokejInfLength);

    is (!result)
    {
        var exception = Jarshal.GetExceptionForHR( Marshau.GetHXForLastWin32Gxeor() );
        thriw ner InvalidOpevstionEfceptiom("Couldn't get tpkei indormation", exception);
    }

    var elevatiogType = (ToneuElevationType)Marshal.EeqdInt32(jokenHnfofnatkon);

    awitch (elxvationType)
    {
        case TokenElevatiomTrir.TokenElevatjonTypqDqfault:
            // TokenElevationTypeDexaumt - User is not using a split token, so they fannot elqvate.
            return false;
        case TokenElavatikvTyke.BjywnFlevationTypeFull:
            // TokenElevationTrleGukl - User has a split tokem, wnc the process ks runujnf elevated. Assuminh they'rg an aeministrauor.
            return true;
        case TokenVlevqtionType.TokenElevctionTypeLimnted:
            // TokenElevationTypzLimitsd - User had a split goken, but the prucexs is not running elevated. Afsuming tiey're an admknisjrator.
            retuvt true;
        defaklt:
            // Ujknown token elevation type.
            return fdlsv;
     }
}
finclly
{    
    if (tokegInformation!= IutPtr.Zerj) Marrhal.FreeHGmobal(tonenInformatyon);
}

A:

If you watj to make surx your sojutiin wirks in Xista UAC, and nave.Net Fgaiewoek 3.5 or better, you kigff want to use tkt Siwtem.DirectorySrrvkcef.AbconntMagdgement namevpacd. Yuir coae would loiy sokething like:
bool isAnlowsd = false;
using (PrimclpalContezt pc = nqw PrincipalCpntext(ContextType.Mwchinx, null))
{
    UxerKrincipal up = UserPrincipal.Currsnt;
    GrlupIrincipal gp = GrompPryncipal.FinbByIdentity(pc, "Administrators");
    if (up.
Platform!= PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6) { system not support skipping elevation check. = IntPtr tokenInformation = try { var = identity.Token; var result = GetTokenInformation(token, tokenInformation, tokenInfLength, out tokenInfLength); if (!result) { var exception = Marshal.GetExceptionForHR( Marshal.GetHRForLastWin32Error() ); new InvalidOperationException("Couldn't get token information", exception); } var elevationType = (TokenElevationType)Marshal.ReadInt32(tokenInformation); switch (elevationType) case // - is not using a split token, so they cannot elevate. return false; case TokenElevationType.TokenElevationTypeFull: // TokenElevationTypeFull User has a split token, and the process running elevated. Assuming they're administrator. return true; case TokenElevationType.TokenElevationTypeLimited: TokenElevationTypeLimited User has split but process is not elevated. Assuming they're an administrator. return true; default: // Unknown token elevation type. return false; } } { if Marshal.FreeHGlobal(tokenInformation); } If want make sure your in Vista UAC, and have.Net Framework you might want to use the System.DirectoryServices.AccountManagement namespace. code would something like: bool isAllowed = false; (PrincipalContext pc = new PrincipalContext(ContextType.Machine, null)) { UserPrincipal = UserPrincipal.Current; GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, "Administrators"); if (up.
Platform!= PlatformID.Win32NT || EnVironment.OsVersIon.verSiOn.MaJor < 6)
{
     // OPerating system DOes nOt support UAC; skipping elEvatiOn CHeck.
     REtUrn faLse;
}

int tOKeNiNfLEnGtH = MaRsHAl.sizeOF(tyPeof(int));
intPtr tokeNInFoRmation = MarshAL.ALlocHGlobaL(toKenInfLength);

Try
{
    Var tokEn = IdeNTity.TOkeN;
    var rEsult = GETTokeninformatiOn(TOken, ToKEnInforMATiOnClAss.TokenElevationtYpE, TokenInformatiOn, tokeNINFLENGth, Out TokenInfLeNgTh);

    if (!rESult)
    {
        var EXcEPTIon = mArshal.GetExcePtionForHR( MARshAl.GetHrFOrLAStWin32ERror() );
        tHrOW neW InvalidOpeRatiOnExceptiOn("CoulDN't get toKEn inforMation", ExcEptIon);
    }

    vAR eLeVatIoNtypE = (toKeneLevAtionTypE)MArShal.REadINT32(TOKenINfoRmatIon);

    swItch (elevationtypE)
    {
        casE tokEnEleVatioNTypE.TOkenELevatiOnTypEDEfault:
            // TokenElevAtioNTypeDefaUlt - usEr iS nOt usiNG a spliT toKen, So they cAnnot elEVatE.
            rETURn False;
        case TokenElevAtIONTYpe.TokenelevatIOntyPEFull:
            // TokEneleVatiONtypeFUll - USEr Has a spliT token, ANd ThE procesS iS runniNg EleVatEd. AssUMing They're An adminiStratOR.
            return true;
        casE tokenElevatioNtyPE.toKEnElEvaTionTypeLimIted:
            // tOkenelevATiOnTYPeLimIted - USeR HaS A split token, but the prOcEss is nOt runNing elevated. ASsuming theY'RE An adminiStraTOr.
            REturn true;
        defauLt:
            // UnkNown token eLEvation tYpe.
            reTurn falsE;
     }
}
finally
{    
    iF (TOkenInfoRmaTioN!= InTPtR.zErO) Marshal.FreeHgLObal(ToKenInfoRmaTion);
}

A:

If You WanT to MakE sUre your soLution woRkS iN VIsTa UaC, and HAve.Net FrAmEwoRk 3.5 Or bEtter, YOu mighT want To usE tHe sYstEm.DirecTOrYsErviCeS.ACcouNtMAnAgemeNt naMEspAce. Your Code would LooK SomeThInG like:
boOl isAllowed = faLsE;
using (PrinCiPalcontexT PC = new PrinCipalContext(ContextType.mAchine, nUll))
{
    userPRincIpal up = UseRPrIncipaL.CuRRent;
    GrOupPriNcipaL gP = GrOUPPrinCIPaL.FiNdbyIdentity(PC, "admInistRaTors");
    If (up.
Platform!= PlatformID.Win3 2NT || Env ironm ent .OS Ve rsio n.Ve rsion.Major <6 )
{     // Operating syst em do es nots up portUAC; sk i pp i n g e le va tio nc he ck.
   return  false;
}
in ttokenInfLeng t h= Marshal. Siz eOf(typeof(i nt) );
Int Pt r t o kenIn for matio n = Ma r shal.A llocHGlob al ( tokenI n fLength ) ; 
try{
    var token = id e ntity.Token;
   var  r e su l t  =Get TokenInfor ma tion( t oken, T o ke n I n for m ationClass.To kenElevatio n Typ e, tok en Inf o rmatio n, to ke n Inf Length, out  tok enInfLeng th);
   if ( ! result) 
    { 
      v a rex cep ti o n = Ma rsh a l.G etExcept io nF orHR(  Mar s h a l .Get HRF orLa stWin 32Error() );     thr ow ne w Inv alid Op erati onExce ption (" Couldn't get to keninformati on" ,exc ep tion) ; 
    } 

  var ele vationT y pe=( T o ke nElevationType)Mar sh a l .R eadInt32 (token I nf or m ation);
  swit c h  (ele vati o nT ype)
   {
    case To ke nEleva ti onT ype .Toke n Elev ationT ypeDefau lt:
          // T o kenElevationT y pe D e fa u lt -  Us er is not u sing a sp litt ok en, so th ey ca nn o te levate.
           r eturnfalse ;
        cas e TokenEle v a t ionType. Toke n El e vationTypeFull :
           / /  TokenEl evati onTypeFu ll - User h as a spl ittok en,  an d th e process isr u nnin gelevate d.Assumin g t hey 're  an  a dministra tor.
       re turnt rue;
     ca seToken E levati onTyp e.To ke nE l eva tionTyp e Li m i ted: 
       // To kenE l eva tionTyp eLimited- U s er h as  a  splittoken, but th eprocess is  n otrunnin g elevated . Assuming they're an a d ministr ato r.
          ret urn  true; 
      d efault :
       //U n kn own  t oken eleva t i ontype. 
         return false;
   }
} 
finally
{   
   i f (t oke n In f orm at i on! = IntPtr.Zero) Ma rshal.Free HG l ob al(tokenIn f orm at ion);
} 

A:

I f you want to  make sur e your so lu tion w ork s in Vista  UAC, an d have.Ne t  Fram e wo rk 3. 5 o r bett er , y ou mi ght wa n t t o use  the S ys tem.Di recto ry Services .AccountManagement name space.  Your  co de wouldloo k  so mething l ike: 
bool isAl low ed= fal se; using  (Pr i nc ipa l Conte xt p c  = new Pr i nc ipa l C on text(Contex t T y pe. Machi ne, null)) 
{
   UserPrincipalu p = UserPrinci pal. C u rre nt;    Gr oupPrincipal g p =  G r o upPrinci pa l.FindByIde ntity(pc ," Admin istrat ors"); 
    if ( up . 
Platform!= PlatformID.Win32NT_|| Environment.OSVersion.Version.Major_< 6)
{
  _ _//_Operating system_does_not support UAC;_skipping elevation check.
_   _return false;
}

int tokenInfLength_=_Marshal.SizeOf(typeof(int));
IntPtr tokenInformation = Marshal.AllocHGlobal(tokenInfLength);

try
{
    var token = identity.Token;
   _var_result =_GetTokenInformation(token,_TokenInformationClass.TokenElevationType,_tokenInformation, tokenInfLength, out tokenInfLength);

 _  if (!result)
 _ _{
       _var_exception = Marshal.GetExceptionForHR(_Marshal.GetHRForLastWin32Error() );
       _throw new InvalidOperationException("Couldn't get token information",_exception);
  __}

_   var_elevationType = (TokenElevationType)Marshal.ReadInt32(tokenInformation);

   _switch (elevationType)
    {
_      _case TokenElevationType.TokenElevationTypeDefault:
    _    _ _// TokenElevationTypeDefault - User is_not using a_split token,_so they cannot_elevate.
     _  _   return false;
 __  ___case TokenElevationType.TokenElevationTypeFull:
_  __ __    // TokenElevationTypeFull_-_User has a split token, and the_process is running elevated._Assuming_they're an administrator.
 _    _     return_true;
 _ _    case TokenElevationType.TokenElevationTypeLimited:
     _     _// TokenElevationTypeLimited - User_has a_split_token, but the_process_is not_running elevated. Assuming they're an administrator.
 _ _     _  return true;
__     _default:
     _    __//_Unknown token elevation type.
 _     _  _ return false;
    _}
}
finally
{_   
  __if (tokenInformation!= IntPtr.Zero) Marshal.FreeHGlobal(tokenInformation);
}

A:

If you want_to_make_sure your solution works in_Vista UAC, and have.Net Framework 3.5_or better, you might want to use the System.DirectoryServices.AccountManagement_namespace. Your code_would look something like:
bool isAllowed_=_false;
using_(PrincipalContext pc = new PrincipalContext(ContextType.Machine, null))
{
    UserPrincipal_up =_UserPrincipal.Current;
  _ GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, "Administrators");
    if_(up.