c# - Detect if running as Administrator with or without elevated privileges? -


i have application needs detect whether or not running elevated privileges or not. have code set this:

static bool isadministrator() {     windowsidentity identity = windowsidentity.getcurrent();     windowsprincipal principal = new windowsprincipal(identity);     return principal.isinrole (windowsbuiltinrole.administrator); } 

this works detect if user administrator or not, doesn't work if running administrator without elevation. (for example in vshost.exe).

how can determine whether or not elevation [already in force or] possible?

try out:

using microsoft.win32; using system; using system.diagnostics; using system.runtime.interopservices; using system.security.principal;  public static class uachelper {     private const string uacregistrykey = "software\\microsoft\\windows\\currentversion\\policies\\system";     private const string uacregistryvalue = "enablelua";      private static uint standard_rights_read = 0x00020000;     private static uint token_query = 0x0008;     private static uint token_read = (standard_rights_read | token_query);      [dllimport("advapi32.dll", setlasterror = true)]     [return: marshalas(unmanagedtype.bool)]     static extern bool openprocesstoken(intptr processhandle, uint32 desiredaccess, out intptr tokenhandle);      [dllimport("advapi32.dll", setlasterror = true)]     public static extern bool gettokeninformation(intptr tokenhandle, token_information_class tokeninformationclass, intptr tokeninformation, uint tokeninformationlength, out uint returnlength);      public enum token_information_class     {         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     }      public enum token_elevation_type     {         tokenelevationtypedefault = 1,         tokenelevationtypefull,         tokenelevationtypelimited     }      public static bool isuacenabled     {                 {             registrykey uackey = registry.localmachine.opensubkey(uacregistrykey, false);             bool result = uackey.getvalue(uacregistryvalue).equals(1);             return result;         }     }      public static bool isprocesselevated     {                 {             if (isuacenabled)             {                 intptr tokenhandle;                 if (!openprocesstoken(process.getcurrentprocess().handle, token_read, out tokenhandle))                 {                     throw new applicationexception("could not process token.  win32 error code: " + marshal.getlastwin32error());                 }                  token_elevation_type elevationresult = token_elevation_type.tokenelevationtypedefault;                  int elevationresultsize = marshal.sizeof((int)elevationresult);                 uint returnedsize = 0;                 intptr elevationtypeptr = marshal.allochglobal(elevationresultsize);                  bool success = gettokeninformation(tokenhandle, token_information_class.tokenelevationtype, elevationtypeptr, (uint)elevationresultsize, out returnedsize);                 if (success)                 {                     elevationresult = (token_elevation_type)marshal.readint32(elevationtypeptr);                     bool isprocessadmin = elevationresult == token_elevation_type.tokenelevationtypefull;                     return isprocessadmin;                 }                 else                 {                     throw new applicationexception("unable determine current elevation.");                 }             }             else             {                 windowsidentity identity = windowsidentity.getcurrent();                 windowsprincipal principal = new windowsprincipal(identity);                 bool result = principal.isinrole(windowsbuiltinrole.administrator);                 return result;             }         }     } } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -