I have a WinForms application and want to be able to offer users an update if one is available (download and install .msi) and if possible. Some users are part of our domain, interns can be part of the domain of school, university or have a personal laptop. I want to detect if a user has administrator rights and is able to perform an update anyway.
I've seen solutions making use of WMI, "SELECT * FROM Win32_UserAccount" (tried, didn't work or maybe I'm missing something). I've seen solution querying the active directory (doesn't work if user has taken laptop home, disconnected). The below solution always returns false and I don't want to require my application to run in elevated mode.
public static bool IsAdministrator()
{
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}