Managed C++: Determining User Security Roles
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
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.
0 Comments (click to add your comment)
Networking Solutions
More for Developers
On the Codeguru Forums
Visit the Forums »Featured Partner Resources
Get your Android Apps ready for Intel® Atom™ processor-based smartphones and tablets now.
Use the Android NDK to deliver the best performance on Intel® Atom™ processor-based devices.
The Android community on the Intel® Software Network has everything you need to prepare your apps for Intel® Atom™ processor-based devices.



Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.