Managed C++: Retrieving User's Windows Security Information

I recently finished up a spyware-detection application that needed to determine various Windows security information such as whether or not the user has administrator rights. Thankfully, I was able to write the application in .NET so I had access to a few extremely helpful classes that made this task very easy. One of those classes is WindowsIdentity (found in the System::Security::Principal namespace). By using this class, I was able to retrieve security information such as the name of the user (including the domain or workgroup), whether or not the user is authenticated, and the authentication type. The following commented code shows how easy this task is:

  1. Include the necessary namespace:
    using namespace System::Security::Principal;
  2. Obtain the WindowsIdentity object associated with the current user:
  3. WindowsIdentity* identity = WindowsIdentity::GetCurrent();
  4. You now can query the WindowsIdentity object's property for the exact information you need:
    listSecurity.SetItemText(0, 1, CString(identity->Name));
    listSecurity.SetItemText(1, 1, CString(identity->AuthenticationType));
    listSecurity.SetItemText(2, 1, CString(identity->IsAuthenticated.ToString()));
    listSecurity.SetItemText(3, 1, CString(identity->IsAnonymous.ToString()));
    listSecurity.SetItemText(4, 1, CString(identity->IsGuest.ToString()));
    listSecurity.SetItemText(5, 1, CString(identity->IsSystem.ToString()));
    listSecurity.SetItemText(6, 1, CString(identity->Token.ToString()));
    

Looking Ahead

The next article will take this a step further and illustrate how you can verify whether the current user is in a specific group (such as the Administrator group), so that you can easily test whether a user has the necessary rights to invoke various functions in a given application.



About the Author

Tom Archer - MSFT

I am a Program Manager and Content Strategist for the Microsoft MSDN Online team managing the Windows Vista and Visual C++ developer centers. Before being employed at Microsoft, I was awarded MVP status for the Visual C++ product. A 20+ year veteran of programming with various languages - C++, C, Assembler, RPG III/400, PL/I, etc. - I've also written many technical books (Inside C#, Extending MFC Applications with the .NET Framework, Visual C++.NET Bible, etc.) and 100+ online articles.

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Live Event Date: May 7, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT This eSeminar will explore three popular games engines and how they empower developers to create exciting, graphically rich, and high-performance games for Android® on Intel® Architecture. Join us for a deep dive as experts describe the features, tools, and common challenges using Marmalade, App Game Kit, and Havok game engines, as well as a discussion of the pros and cons of each engine and how they fit into your development …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds