http://www.developer.com/security/article.php/3461401/Managed-C-Determining-User-Security-Roles.htm
In my previous article ("Managed C++: Retrieving User's Windows Security Information"), I mentioned that there are times when an application can benefit from knowing specific Windows security information about a user. For example, in a recent spyware detection/removal system that I wrote, the code needed to delete certain files and, if those files were in use, mark them for deletion via the registry. This latter part involved changing certain registry keys that required that the user be defined in the Administrator
group.
This article illustrates how to use the WindowsIdentity and WindowsPrincipal classes to test for a user's inclusion in a specified security group and how to use the PrincipalPermission class to perform a security check against the active principal.
Click here for a larger image.
Tom Archer owns his own training company, Archer Consulting Group, which specializes in educating and mentoring .NET programmers and providing project management consulting. If you would like to find out how the Archer Consulting Group can help you reduce development costs, get your software to market faster, and increase product revenue, contact Tom through his Web site.
Managed C++: Determining User Security Roles
January 24, 2005
Determining Role
The .NET security classes enable you to determine both authentication information regarding a user and specific role information (see Figure 1).

using namespace System::Security::Principal;
WindowsIdentity* identity = WindowsIdentity::GetCurrent();
WindowsPrincipal* principal = new WindowsPrincipal(identity);
bool isAdmin = principal->IsInRole(WindowsBuiltInRole::Administrator);
Using the PrincipalPermissions Object
Another way to check for the inclusion of a user in a security group is by using the PrincipalPermission class, which allows you to perform a security check against the active principal:
using namespace System::Security::Permissions;
using namespace System::Threading;
AppDomain* dom = AppDomain::CurrentDomain;
dom->SetPrincipalPolicy(PrincipalPolicy::WindowsPrincipal);
WindowsIdentity* identity = WindowsIdentity::GetCurrent();
PrincipalPermission* permissions
= new PrincipalPermission(identity->Name, "Administrators");
try
{
permissions->Demand();
//... run code that requires the checked-for rights
}
catch(Security::SecurityException* ex)
{
// ex->Message will contain the exact error message
}
Testing for Inclusion in One of Multiple Groups
If the code you're attempting to execute can be executed by someone belonging to any of multiple groups, you can use the PrincipalPermission::Union method to join these groups and then call the PrincipalPermission::Demand method, which will throw an exception only if the user doesn't belong to any of those groups. Here's an example of this using code from the previous section. I've bolded the changes:
try
{
AppDomain* dom = AppDomain::CurrentDomain;
dom->SetPrincipalPolicy(PrincipalPolicy::WindowsPrincipal);
WindowsIdentity* identity = WindowsIdentity::GetCurrent();
PrincipalPermission* permissions = new PrincipalPermission(identity->Name, "Administrators");
PrincipalPermission* permissionsPU = new PrincipalPermission(identity->Name, "PowerUsers");
permissions->Union(permissionsPU);
permissions->Demand();
//... run code that requires the checked-for rights
}
catch(Security::SecurityException* ex)
{
// ex->Message will contain the exact error message
}
Security for the Rest of Us
Not many of us are security gurus like Keith Brown (my favorite author and trainer on the subject). However, with these past two articles, you can perform some very basic security-rights verification without having to become an expert on Windows security. If you do wish to learn more, I would highly recommend any of Keith's books on the subject.
About the Author