AccessToken Class
- Supported on windows5.1.2600
Represents a Windows access token that identifies a security context for a process or thread.
[SupportedOSPlatform("windows5.1.2600")]
public sealed class AccessToken : IDisposableExample
// Open the current process token. Adjusting privileges requires the AdjustPrivileges access right.
using var token = AccessToken.OpenCurrentProcessToken(TokenAccessLevels.Query | TokenAccessLevels.AdjustPrivileges);
// Check if the process is elevated
bool isElevated = token.IsElevated();
Console.WriteLine($"Elevated: {isElevated}");
// Get token information
Console.WriteLine($"Token Type: {token.GetTokenType()}");
Console.WriteLine($"Elevation Type: {token.GetElevationType()}");
Console.WriteLine($"Owner: {token.GetOwner()}");
// Enumerate groups
foreach (var group in token.EnumerateGroups())
{
Console.WriteLine($"Group: {group.Sid} ({group.Attributes})");
}
// Enumerate privileges
foreach (var privilege in token.EnumeratePrivileges())
{
Console.WriteLine($"Privilege: {privilege.Name} ({privilege.Attributes})");
}
// Enable a privilege. The token must hold it: SeDebugPrivilege is only granted to administrators.
token.EnablePrivilege(Privileges.SE_DEBUG_NAME);- Implements
Methods
| Name | Description |
|---|---|
| DisableAllPrivileges() | Disables all privileges for this token. |
| DisablePrivilege(string) | Disables a privilege for this token. |
| Dispose() | Releases the unmanaged resources used by the AccessToken. |
| Duplicate(SecurityImpersonationLevel) | Creates a duplicate of this token with the specified impersonation level. |
| EnablePrivilege(string) | Enables a privilege for this token. |
| EnumerateGroups() | Enumerates all groups (security identifiers) associated with the token. |
| EnumeratePrivileges() | Enumerates all privileges held by the token. |
| EnumerateRestrictedSid() | Enumerates the restricted SIDs in the token. |
| GetElevationType() | Gets the elevation type of the token. |
| GetLinkedToken() | Gets the linked token associated with this token. |
| GetMandatoryIntegrityLevel() | Gets the mandatory integrity level of the token. |
| GetOwner() | Gets the owner security identifier (SID) of the token. |
| GetTokenType() | Gets the type of the token (primary or impersonation). |
| IsElevated() | Determines whether the token is elevated (running with administrative privileges). |
| IsLimitedToken() | Determines whether the current process is running with a limited token. |
| IsRestricted() | Determines whether the token is restricted. |
| OpenCurrentProcessToken(TokenAccessLevels) | Opens the access token associated with the current process. |
| OpenProcessToken(Process, TokenAccessLevels) | Opens the access token associated with the specified process. |
| RemovePrivilege(string) | Removes a privilege from this token. |
DisableAllPrivileges()
AccessToken.csDisables all privileges for this token.
public void DisableAllPrivileges()Exceptions
Win32Exception- The operation failed.
DisablePrivilege(string)
AccessToken.csDisables a privilege for this token.
Parameters
privilegeNamestring- The name of the privilege to disable (e.g.,
Privileges.SE_DEBUG_NAME).
Exceptions
ArgumentNullExceptionprivilegeNameisnull.Win32Exception- The privilege could not be disabled.
Dispose()
AccessToken.csReleases the unmanaged resources used by the AccessToken.
public void Dispose()Duplicate(SecurityImpersonationLevel)
AccessToken.csCreates a duplicate of this token with the specified impersonation level.
public AccessToken Duplicate(SecurityImpersonationLevel impersonationLevel)Parameters
impersonationLevelSecurityImpersonationLevel- The impersonation level for the duplicate token.
Returns
AccessToken A new AccessToken representing the duplicate.
Exceptions
Win32Exception- The token could not be duplicated.
EnablePrivilege(string)
AccessToken.csEnables a privilege for this token.
Parameters
privilegeNamestring- The name of the privilege to enable (e.g.,
Privileges.SE_DEBUG_NAME).
Exceptions
ArgumentNullExceptionprivilegeNameisnull.Win32Exception- The privilege could not be enabled.
EnumerateGroups()
AccessToken.csEnumerates all groups (security identifiers) associated with the token.
public IEnumerable<TokenGroupEntry> EnumerateGroups()Returns
IEnumerable<TokenGroupEntry> A collection of TokenGroupEntry objects representing the groups.
EnumeratePrivileges()
AccessToken.csEnumerates all privileges held by the token.
public IEnumerable<TokenPrivilegeEntry> EnumeratePrivileges()Returns
IEnumerable<TokenPrivilegeEntry> A collection of TokenPrivilegeEntry objects representing the privileges.
EnumerateRestrictedSid()
AccessToken.csEnumerates the restricted SIDs in the token.
public IEnumerable<TokenGroupEntry> EnumerateRestrictedSid()Returns
IEnumerable<TokenGroupEntry> A collection of TokenGroupEntry objects representing the restricted SIDs.
Remarks
Restricted SIDs are used to limit the token's access to resources beyond the standard access checks.
GetElevationType()
AccessToken.csGets the elevation type of the token.
public TokenElevationType GetElevationType()Returns
TokenElevationType A TokenElevationType value indicating the elevation status.
Remarks
The elevation type indicates whether the token is elevated, limited, or default. This is useful for User Account Control (UAC) scenarios.
GetLinkedToken()
AccessToken.csGets the linked token associated with this token.
public AccessToken? GetLinkedToken()Returns
AccessToken? The linked AccessToken, or null if no linked token exists.
Remarks
In UAC scenarios, an administrator account has two tokens: a limited token for normal operations and an elevated token for administrative tasks. This method retrieves the linked token.
GetMandatoryIntegrityLevel()
AccessToken.csGets the mandatory integrity level of the token.
public TokenEntry GetMandatoryIntegrityLevel()Returns
TokenEntry A TokenEntry containing the integrity level SID.
Remarks
The mandatory integrity level (e.g., Low, Medium, High, System) determines what resources the token can access.
GetOwner()
AccessToken.csGets the owner security identifier (SID) of the token.
public SecurityIdentifier GetOwner()Returns
SecurityIdentifier The SecurityIdentifier of the token owner.
GetTokenType()
AccessToken.csIsElevated()
AccessToken.csIsLimitedToken()
AccessToken.csDetermines whether the current process is running with a limited token.
public static bool IsLimitedToken()Returns
bool true if the current process is running with a limited token; otherwise, false.
Remarks
A limited token is created when User Account Control (UAC) is enabled and an administrator logs in. The limited token has fewer privileges than the full administrative token.
IsRestricted()
AccessToken.csOpenCurrentProcessToken(TokenAccessLevels)
AccessToken.csOpens the access token associated with the current process.
public static AccessToken OpenCurrentProcessToken(TokenAccessLevels accessLevels)Parameters
accessLevelsTokenAccessLevels- The access levels required for the token.
Returns
AccessToken An AccessToken representing the current process token.
Exceptions
Win32Exception- The token could not be opened.
OpenProcessToken(Process, TokenAccessLevels)
AccessToken.csOpens the access token associated with the specified process.
public static AccessToken OpenProcessToken(Process process, TokenAccessLevels accessLevels)Parameters
processProcess- The process whose token to open.
accessLevelsTokenAccessLevels- The access levels required for the token.
Returns
AccessToken An AccessToken representing the process token.
Exceptions
ArgumentNullExceptionprocessisnull.Win32Exception- The token could not be opened.
RemovePrivilege(string)
AccessToken.csRemoves a privilege from this token.
Parameters
privilegeNamestring- The name of the privilege to remove (e.g.,
Privileges.SE_DEBUG_NAME).
Exceptions
ArgumentNullExceptionprivilegeNameisnull.Win32Exception- The privilege could not be removed.