Click here to Skip to main content
11,929,675 members (58,848 online)
Click here to Skip to main content
Add your own
alternative version


75 bookmarked

NT Security Classes for .NET

, 19 Feb 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
A collection of .NET classes written in Managed C++ that faciliate the manipulation of NT security rights


This class library allows access to the Win32 security calls in a .NET friendly way. It encapsulates the concepts of a user, a securable object (like a file, named pipe, directory, etc.), and permissions. This library was written in Managed C++ to simplify the amount of work needed to link to existing Win32 libraries. However, since it exposes all of its functionality via .NET, it can be used from any .NET compliant language, including C# and Visual Basic. The project was written and compiled with Visual Studio 2002.

NOTE: There is a library written by some Microsoft guys on GotDotNet that does much of the same thing and more. It can be found at

This article outlines the primary objects in the library and their use in manipulating security objects.


WindowsUser class

This class represents a single Windows identity (SID). It can be created by specifying either a username ("DOMAIN\user" format) or the string representation of a SID ("S-1-5-xxxx-xxx..."). You can also get the identity of the current user using the static property CurrentUser.

There are a number of predefined identities that exist as static members of a child class called WellKnownIdentities. Once you have an identity, you can get the following properties:

  • AccountName: string name of the account
  • Domain: string name of the account's domain
  • FullName: string in the form of "Domain\AccountName"
  • SidString: string representation of the SID

SecuredObject class

This class represents an object which can have a security descriptor. It can be created by specifying the name of the resource along with its type or by passing a handle (as an IntPtr) to the resource.

Once you have the object, you can update the permissions, audit information, owner and group.


This class encapsulates actions on the ACL. It allows granting, revoking, changing, and denying access levels to different users. Derived from AccessList, which is a collection class for AccessEntry.


This class encapsulates actions on the auditing list of an object. It allows getting and setting audit success and failure rights. Derived from AccessList, which is a collection class for AccessEntry.


This class encapsulates the Access Control Entry or ACE. You can set the user (trustee) and the associated rights and inheritance.


This code shows the library in action. It assumes you have aliased the Microsoft.Win32.Security namespace (using in C#, Imports in VB).

// Get the current user and print their information
WindowsUser user = WindowsUser.CurrentUser;
Console.WriteLine("{0} ({1})", user.FullName, user.SidString);

// Get the current user from their token
WindowsUser duser = new WindowsUser(

// Compare users
if (user == duser)

// Get a well-known user
user = WindowsUser.WellKnownIdentities.World;

// Get a user by name from a specific server (usually a domain controller)
WindowsUser kuser = new WindowsUser("user2", @"\\MYPDC");

// Get a user by name
user = new WindowsUser("DOMAIN\\user3");

// Get a user by SID
user = new WindowsUser("S-1-5-21-21782756-1035017279-1439700725-1111");

// Get security for C:\ directory
SecuredObject sec = new SecuredObject("C:\\", SecuredObjectType.FileObject);

// Set some various permissions on the directory
sec.Permissions.SetAccess(kuser, AccessRights.FileRead,
sec.Permissions.GrantAccess(kuser, AccessRights.FileExecute,
sec.Permissions.DenyAccess(kuser, AccessRights.FileWriteUnsync,
WindowsUser owner = sec.Owner;
sec.Owner = duser;
sec.Auditing.SetAuditFailure(duser, AccessRights.FileReadUnsync,

// Revoke some access
sec.Owner = owner;
DumpObject(sec) ;

// Reset the security on the directory
sec.Permissions.InheritFromParent = true;

// Write the DACL using the Microsoft style

The following function shows how to enumerate the permissions on a security object.

static void DumpObject(SecuredObject sec)
   Console.WriteLine("Security description:");
   Console.WriteLine("Owner: {0}\nGroup: {1}", 
     sec.Owner.FullName, sec.Group.FullName);
   foreach (AccessEntry ace in sec.Permissions)
      Console.WriteLine(String.Format("  {0} : {1} : {2}", 
         ace.Inheritance, ace.Rights));
   foreach (AccessEntry ace in sec.Auditing)
      Console.WriteLine(String.Format("  {0} : {1} : {2}", 
         ace.Inheritance, ace.Rights));


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

David Hall
Chief Technology Officer
United States United States
I have been a Windows software developer since 1991. Most of what I create fills the need for some aspect of bigger projects that I consult on.

You may also be interested in...

Comments and Discussions

GeneralRe: unverifiable assembly.... Pin
mpemberton3-Jul-03 8:38
membermpemberton3-Jul-03 8:38 
GeneralRe: unverifiable assembly.... Pin
Bence, Kovari16-Jan-05 0:35
memberBence, Kovari16-Jan-05 0:35 
GeneralProblem getting HKLM permissions Pin
Steve021219-May-03 16:27
memberSteve021219-May-03 16:27 
GeneralRe: Problem getting HKLM permissions Pin
David Hall20-May-03 7:29
memberDavid Hall20-May-03 7:29 
GeneralRe: Problem getting HKLM permissions Pin
Steve021220-May-03 14:57
memberSteve021220-May-03 14:57 
GeneralSecurity Classes from Microsoft Pin
David Hall6-Mar-03 6:54
memberDavid Hall6-Mar-03 6:54 
QuestionCan anyone help me? Pin
parkman5-Mar-03 9:46
memberparkman5-Mar-03 9:46 
AnswerRe: Can anyone help me? Pin
David Hall6-Mar-03 6:38
memberDavid Hall6-Mar-03 6:38 
GeneralBuilding .NET project correctly Pin
parkman4-Mar-03 9:11
memberparkman4-Mar-03 9:11 
GeneralUsing this module in commercial code Pin
Steve021220-Feb-03 16:41
memberSteve021220-Feb-03 16:41 
GeneralRe: Using this module in commercial code Pin
leppie20-Feb-03 16:59
memberleppie20-Feb-03 16:59 
GeneralUnable to load file 'mmsseclib' Pin
CliffeH6-Feb-03 11:07
memberCliffeH6-Feb-03 11:07 
GeneralOh, and one more thing... Pin
CliffeH6-Feb-03 11:37
memberCliffeH6-Feb-03 11:37 
GeneralRe: Oh, and one more thing... Pin
criZasher6-Feb-03 11:46
membercriZasher6-Feb-03 11:46 
GeneralPerfect Pin
pburns19-Nov-02 16:30
memberpburns19-Nov-02 16:30 
Questionregistrykey securedobject? Pin
groovyponens6-Oct-02 23:54
membergroovyponens6-Oct-02 23:54 
AnswerRe: registrykey securedobject? Pin
groovyponens7-Oct-02 0:29
membergroovyponens7-Oct-02 0:29 
Mazdak19-Sep-02 4:34
memberMazdak19-Sep-02 4:34 
GeneralOwners! Pin
Mazdak19-Sep-02 3:18
memberMazdak19-Sep-02 3:18 
GeneralInheritence of permssions Pin
Syborg7712-Sep-02 22:13
memberSyborg7712-Sep-02 22:13 
GeneralRe: Inheritence of permssions Pin
Pierrick17-Sep-02 6:43
memberPierrick17-Sep-02 6:43 
GeneralRe: Inheritence of permssions Pin
Syborg7717-Sep-02 21:43
memberSyborg7717-Sep-02 21:43 
GeneralRe: Inheritence of permssions Pin
Pierrick18-Sep-02 0:00
memberPierrick18-Sep-02 0:00 
GeneralRe: Inheritence of permssions Pin
Syborg7718-Sep-02 22:06
memberSyborg7718-Sep-02 22:06 
GeneralRe: Inheritence of permssions Pin
Pierrick19-Sep-02 7:18
memberPierrick19-Sep-02 7:18 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 20 Feb 2004
Article Copyright 2002 by David Hall
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid