Click here to Skip to main content
12,898,392 members (57,859 online)
Click here to Skip to main content


125 bookmarked
Posted 14 May 2005

The Windows Access Control Model Part 3

, 1 Jul 2005
In the third part of this series, we will take a tour of the new access control classes coming in .NET v2.0.

This reusable class is based on the data from "The Windows Access Control Model - Part 2",
and modelled after the ATL class library.

This class extends the WindowsIdentity class to make it as functional as CAccessToken
(apart from the userprofile/impersonation stuff).

The class was written in C++/CLI. It requires Visual C++ 2005 to compile, and .Net Framework 2.0 (Win2k) to run.
It makes explicit use of classes/methods created for .Net 2.0. It cannot run in an earlier version of the

Error codes have been shunned for Exceptions.

Static Methods
To construct an access token, you will need to an access token to the process. Use this static
function to obtain an access token from a Process.Handle:
ManagedTokenHandle GetAccessToken(IntPtr processHandle = 0, TokenAccessLevels dwDesiredAccess);

Public methods.
void GetEffectiveToken(TokenAccessLevels dwDesiredAccess);
void GetProcessToken(TokenAccessLevels dwDesiredAccess, IntPtr hProcess = 0);
void OpenThreadToken(TokenAccessLevels dwDesiredAccess, bool bImpersonate = false, bool bOpenAsSelf = true, TokenImpersonationLevel Impersonator = Impersonation);
void GetThreadToken(TokenAccessLevels dwDesiredAccess, IntPtr hThread = 0, bool bOpenAsSelf = true);
void SetPrivilege(String privilegeName, bool bEnable);
NameValueCollection GetPrivileges();

These functions have the same definitions as they do in ATL.

Public Properties.
IntPtr Token;
RawAcl DefaultDacl;
SecurityIdentifier PrimaryGroup;
Int64 LogonSessionId;
unsigned long TerminalServicesSessionId;

The following helper classes were needed to create AccessToken.

enum TOKEN_TYPEEnum;
/* Marshaller for TOKEN_TYPE. */

class ManagedTokenHandle : SafeHandle;
/* Automatic Handle management for the access token.  */


CodeProject has a policy (
that governs the copyright of projects posted at its site. This
program bears that copyright.

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
Mr. Shah is a reclusive C++/C# developer lurking somewhere in the depths of the city of London. He learnt physics at Kings' College London and obtained a Master in Science there. Having earned an MCAD, he teeters on the brink of transitioning from C++ to C#, unsure of which language to jump to. Fortunately, he also knows how to use .NET interop to merge code between the two languages (which means he won't have to make the choice anytime soon).

His interests (apart from programming) are walking, football (the real one!), philosophy, history, retro-gaming, strategy gaming, and any good game in general.

He maintains a website / blog / FAQ / junk at, where he places the best answers he's written to the questions you've asked. If you can find him, maybe you can hire Mr. Shah to help you with anything C++[/CLI]/C#/.NET related Smile | :) .

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 1 Jul 2005
Article Copyright 2005 by oshah
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid