For a small utility app I'm writing at work, I have the following code to determine if the current user account is a local or domain administrator:
WCHAR wszUser[UNLEN];
GetEnvironmentVariableW(L"username", wszUser, UNLEN);
#ifndef _DEBUG
if (StrCmpIW(wszUser, L"Administrator") != 0)
{
MessageBoxW(0, L"This program can only be run as Administrator.", L"Error", MB_OK | MB_ICONSTOP);
return 0;
}
#endif
This works in our case right now because:
- The domain admin account we have is the built-in Administrator account
- We don't have any other domain admin accounts.
However, I realize that this is a bad solution because in the future we might add other domain administrator accounts. Is there a way to determine using the Windows API whether the user account that the process is running from belongs to either the Domain Admins group, or BUILTIN\Administrators
, or not?
This is not a security concern since the app won't actually be able to do anything useful unless it's running as an Administrator, this is just for robustness.