Click here to Skip to main content
16,016,249 members
Home / Discussions / C#
   

C#

 
GeneralRuntime type casting Pin
Skippums18-Dec-07 3:23
Skippums18-Dec-07 3:23 
GeneralRe: Runtime type casting Pin
CKnig18-Dec-07 3:29
CKnig18-Dec-07 3:29 
GeneralRe: Runtime type casting Pin
CKnig18-Dec-07 3:32
CKnig18-Dec-07 3:32 
GeneralRe: Runtime type casting Pin
Skippums18-Dec-07 6:37
Skippums18-Dec-07 6:37 
GeneralRe: Runtime type casting Pin
m@u18-Dec-07 3:35
m@u18-Dec-07 3:35 
GeneralRe: Runtime type casting Pin
Skippums18-Dec-07 5:56
Skippums18-Dec-07 5:56 
GeneralDetermine User Role (Vista) Pin
Stevo Z18-Dec-07 3:10
Stevo Z18-Dec-07 3:10 
GeneralRe: Determine User Role (Vista) Pin
Peter Walburn26-Feb-10 4:38
Peter Walburn26-Feb-10 4:38 
I managed to do this by adding this class to my application:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace Gauge
{
public static class mySecurity
{
#region Constants
const UInt32 TOKEN_QUERY = 8;
const int INT_SIZE = 4;
#endregion

#region Enumerations
private enum TOKEN_ELEVATION_TYPE
{
TokenElevationTypeDefault = 1,
TokenElevationTypeFull,
TokenElevationTypeLimited
}

public enum TOKEN_INFO_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
// MaxTokenInfoClass should always be the last enum
}
#endregion

#region WIN API FUNCTIONS
[DllImport("kernel32.dll")]
public static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", SetLastError=true)]
public static extern Boolean OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle);

[DllImport("advapi32.dll", SetLastError=true)]
public static extern Boolean GetTokenInformation(IntPtr TokenHandle, TOKEN_INFO_CLASS TokenInformationClass,
IntPtr TokenInformation, int TokenInformationLength, out uint ReturnLength);
#endregion

#region Public Methods
///
/// Returns True when the current user is a member of the
/// Administrators group and is also running the process
/// elevated as an Administrator, otherwise returns false.
///

/// <returns>
/// <c>true if user is running the process elevated as an Administrator; otherwise, <c>false.
///
public static Boolean IsRunningAsAdmin(WindowsPrincipal pWindowsPrincipal)
{
return pWindowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}

///
/// Checks whether the user can elevate to Administrator rights.
///

/// <returns><c>true if user can elevate to Administrator rights; <c>false otherwise.
public static Boolean CanElevateToAdmin(WindowsPrincipal pWindowsPrincipal)
{
Boolean returnVal = false;

// Determine if the current user is already running with Administrator rights.
Boolean isAdmin = IsRunningAsAdmin(pWindowsPrincipal);

// Return True if the user is an Administrator (user must have Administration rights).
if (isAdmin)
return true;

OperatingSystem os = Environment.OSVersion;

Version vs = os.Version;

// If Vista or Higher, check for split token.
if (vs.Major > 5)
{
try
{
IntPtr myToken;
TOKEN_ELEVATION_TYPE elevationType;
uint dwSize;
IntPtr pElevationType = Marshal.AllocHGlobal(INT_SIZE);

// Get a token reference for the user running this process.
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, out myToken);

// Get the elevation information for this token.
GetTokenInformation(myToken, TOKEN_INFO_CLASS.TokenElevationType, pElevationType,
INT_SIZE, out dwSize);

// Cast the result to ENUM_TYPE.
elevationType = (TOKEN_ELEVATION_TYPE)(Marshal.ReadInt32(pElevationType));

// Free allocated unmanaged memory.
Marshal.FreeHGlobal(pElevationType);

// Determine result of the elevation check.
// ==============================================
// TokenElevationTypeFull - User has a split token,
// and the process is running elevated
// TokenElevationTypeDefault - User is not using a split token
// ==============================================
returnVal = (elevationType == TOKEN_ELEVATION_TYPE.TokenElevationTypeLimited);
returnVal = returnVal || (elevationType == TOKEN_ELEVATION_TYPE.TokenElevationTypeFull);
}
catch
{
returnVal = false;
}
}
else
{
// Prior to Vista, only check needed is if user is in Administrators group.
returnVal = isAdmin;
}

return returnVal;
}

///
/// Checks if user is running as a standared user, but can elevate to Administrator rights.
///

/// <returns><c>true if user can elevate with their own credentials; <c>false otherwise.
GeneralTAPI 2 Pin
baerten18-Dec-07 3:09
baerten18-Dec-07 3:09 
GeneralConvert to int Pin
eyeseetee18-Dec-07 1:55
eyeseetee18-Dec-07 1:55 
GeneralRe: Convert to int Pin
J4amieC18-Dec-07 1:59
J4amieC18-Dec-07 1:59 
GeneralRe: Convert to int Pin
Pete O'Hanlon18-Dec-07 2:28
mvePete O'Hanlon18-Dec-07 2:28 
GeneralRe: Convert to int Pin
eyeseetee18-Dec-07 3:00
eyeseetee18-Dec-07 3:00 
GeneralRe: Convert to int Pin
Vasudevan Deepak Kumar18-Dec-07 3:01
Vasudevan Deepak Kumar18-Dec-07 3:01 
GeneralRe: Convert to int Pin
eyeseetee18-Dec-07 3:13
eyeseetee18-Dec-07 3:13 
GeneralRe: Convert to int Pin
eyeseetee18-Dec-07 3:18
eyeseetee18-Dec-07 3:18 
GeneralUsing WebService saving the files to client system using its Internet IP address Pin
ag4667718-Dec-07 0:30
ag4667718-Dec-07 0:30 
Questioncan we hide focus from grid cell Pin
amit_8318-Dec-07 0:20
amit_8318-Dec-07 0:20 
AnswerRe: can we hide focus from grid cell Pin
Vasudevan Deepak Kumar18-Dec-07 3:00
Vasudevan Deepak Kumar18-Dec-07 3:00 
Questionredirecting Pin
liatma17-Dec-07 23:57
liatma17-Dec-07 23:57 
GeneralRe: redirecting Pin
Paul Conrad22-Dec-07 9:21
professionalPaul Conrad22-Dec-07 9:21 
Questionhow to deactivate grid cell Pin
amit_8317-Dec-07 23:51
amit_8317-Dec-07 23:51 
QuestionWhat this code is doing Pin
Hum Dum17-Dec-07 23:38
Hum Dum17-Dec-07 23:38 
AnswerRe: What this code is doing Pin
AlwiNus17-Dec-07 23:49
AlwiNus17-Dec-07 23:49 
GeneralRe: What this code is doing Pin
Hum Dum18-Dec-07 1:19
Hum Dum18-Dec-07 1:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.