Click here to Skip to main content
Click here to Skip to main content

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

Comments and Discussions

GeneralBroken Link Pinmembercilu11-Dec-09 6:41 
QuestionMemoryleak? Pinmembermackenb13-Oct-09 3:41 
GeneralWin32Helper Fails on comparing two null sids. PinmemberMember 382942711-Aug-09 2:32 
QuestionHow to setup mmsseclib for usage PinmemberJuergen Loewner21-Aug-07 21:43 
GeneralRe: How to setup mmsseclib for usage PinmemberJuergen Loewner22-Aug-07 8:12 
GeneralConsole frontend à la (X)Cacls PinmemberLiQuick28-Mar-07 3:31 
QuestionCompiler problem Pinmemberwakkoedu6-Dec-06 2:27 
QuestionDavid, can I use your dll in the commercial product? PinmemberDmitry V.Trus23-Nov-06 6:07 
GenerallaunchPermission on component Pinmembersuperk10-May-06 1:44 
Generalexception question Pinmemberah_zen11-Mar-06 2:30 
GeneralRe: exception question PinmemberMrBonus1-Aug-06 1:46 
QuestionVB 2005 issue Pinmemberlmwinbur13-Feb-06 19:11 
AnswerRe: VB 2005 issue Pinmemberbbosak23-Mar-06 11:11 
AnswerRe: VB 2005 issue PinmemberOliProject18-Apr-07 4:27 
AnswerRe: VB 2005 issue PinmemberMember 294579031-Dec-08 9:04 
GeneralOn NT4.0 SP6 Machine Pinmemberk_udct3-Jun-05 0:22 
GeneralRe: On NT4.0 SP6 Machine PinmemberDavid Hall6-Jun-05 6:27 
GeneralRe: On NT4.0 SP6 Machine Pinmemberk_udct8-Jun-05 4:54 
GeneralRe: On NT4.0 SP6 Machine PinmemberDavid Hall8-Jun-05 5:43 
GeneralError while assigning SecurityObject PinmemberVikramSaraf30-May-05 22:07 
GeneralRe: Error while assigning SecurityObject PinmemberDavid Hall6-Jun-05 6:30 
GeneralNamed pipe help Pinmembererictaneda26-Dec-04 9:45 
GeneralQuestion about enums.h PinmemberJohn Rusk29-Nov-04 17:36 
GeneralRe: Question about enums.h PinmemberDavid Hall30-Nov-04 5:51 
GeneralRe: Question about enums.h PinmemberJohn Rusk30-Nov-04 9:32 
GeneralAdds special permissions not regula permissions Pinmemberpatel_o19-Nov-04 5:54 
GeneralRe: Adds special permissions not regula permissions PinmemberDavid Hall19-Nov-04 6:08 
GeneralRe: Adds special permissions not regula permissions Pinmemberpatel_o19-Nov-04 9:36 
GeneralRe: Adds special permissions not regula permissions PinmemberDavid Hall7-Dec-04 8:07 
GeneralRe: Adds special permissions not regula permissions PinmemberLiQuick9-May-06 2:16 
GeneralRegistry ACLs PinmemberJames Miles / 20810974316-Nov-04 19:10 
AnswerRe: Registry ACLs Pinmemberfuzzylintman29-Sep-05 7:02 
GeneralNull Reference PinmemberXe Capital25-Aug-04 7:20 
QuestionGetEffectiveRights(user) does it work? Pinmembergryfgryf26-Jun-04 12:38 
GeneralAccessRights Error! PinmemberReisses5-Feb-04 10:06 
GeneralRe: AccessRights Error! PinmemberDavid Hall5-Feb-04 11:13 
GeneralRe: AccessRights Error! PinmemberReisses5-Feb-04 11:24 
GeneralRe: AccessRights Error! PinmemberDavid Hall5-Feb-04 11:28 
GeneralRe: AccessRights Error! PinmemberReisses5-Feb-04 11:31 
GeneralRe: AccessRights Error! Pinmemberjohnleonard30-Sep-04 5:48 
GeneralCan't build Pinmemberthe_cRaCk_3-Dec-03 4:43 
GeneralRe: Can't build PinmemberDavid Hall3-Dec-03 5:12 
GeneralRe: Can't build Pinmembergdumas7-Dec-03 23:48 
GeneralRe: Can't build Pinmemberthe_cRaCk_14-Jan-04 0:02 
here is the solution:

goto the mmsseclib.h and remove the <lm.h> include statement
#include "enums.h"
#include "mgdhelp.h"
//#include <lm.h>

Smile | :)

GeneralRe: Can't build PinmemberXe Capital23-Jul-04 8:38 
GeneralRe: Can't build PinmemberDavid Hall29-Jul-04 6:29 
GeneralRe: Can't build Pinmembergibneys13-Jul-05 4:07 
GeneralGet file security with Win32Security from Microsoft Pinmemberportyr24-Oct-03 1:43 
Generalunverifiable assembly.... Pinmemberroglan10-Jun-03 0:12 
GeneralRe: unverifiable assembly.... PinmemberDavid Hall10-Jun-03 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web04 | 2.8.150414.1 | Last Updated 20 Feb 2004
Article Copyright 2002 by David Hall
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid