Question
Identify the user running the application
Solution
#include "stdafx.h"
using namespace System;
using namespace System::Security;
using namespace System::Security::Claims;
using namespace System::Security::Principal;
int main(array<System::String ^> ^args)
{
AppDomain::CurrentDomain->SetPrincipalPolicy(Principal::PrincipalPolicy::WindowsPrincipal);
auto principal = dynamic_cast<WindowsPrincipal^>(WindowsPrincipal::Current);
if(principal == nullptr){
Console::WriteLine("Failed in converting WindowsPrincipal::Current");
return 1;
}
auto identity = dynamic_cast<WindowsIdentity^>(principal->Identity);
if(identity == nullptr){
Console::WriteLine("Failed in converting principal->Identity");
return 1;
}
Console::WriteLine("IdentityType: {0}", identity->ToString());
Console::WriteLine("Name: {0}", identity->Name);
Console::WriteLine("'Users'?: {0} ", principal->IsInRole(Principal::WindowsBuiltInRole::User));
Console::WriteLine("'Administrators'? {0}", principal->IsInRole(Principal::WindowsBuiltInRole::Administrator));
Console::WriteLine("Authenticated: {0}", identity->IsAuthenticated);
Console::WriteLine("AuthType: {0}", identity->AuthenticationType);
Console::WriteLine("Anonymous? {0}", identity->IsAnonymous);
Console::WriteLine("Token: {0}", identity->Token);
return 0;
}
Output
IdentityType: System.Security.Principal.WindowsIdentity
Name: ********\****
'Users'?: True
'Administrators'? False
Authenticated: True
AuthType: LiveSSP
Anonymous? False
Token: 536
Press any key to continue . . .