Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / Visual Basic

The Windows Access Control Model Part 3

Rate me:
Please Sign up or sign in to vote.
4.80/5 (28 votes)
1 Jul 200525 min read 232.9K   5.2K   126  
In the third part of this series, we will take a tour of the new access control classes coming in .NET v2.0.
AccessToken.AccessToken
---------------------------------------------

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
framework.

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;
TOKEN_TYPEEnum Type;
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.  */


Usage.
---------------------------------------------

CodeProject has a policy (http://www.codeproject.com/info/faq.asp)
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.

License

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


Written By
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 shexec32.serveftp.net, 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 | :) .

Comments and Discussions