I have code to verify if user is present in administrator group on local machine. The code works fine if user is directly present in administrator group
using (DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./Administrators,group")) {
foreach (object member in (IEnumerable)groupEntry.Invoke("Members"))
{
using (DirectoryEntry memberEntry = new DirectoryEntry(member))
{
if (memberEntry.Name.ToLower() == UserName.ToLower())
{
IsUserAdmin = true;
break;
}
}
} }
But the code fails if user is present in an AD group and that AD group is added in administrator group. Another case is user is part of nested AD group and the final AD group is added in administrator group.
How can we check if user is part of administrator group when he is directly added and when related AD group is present?
I want to make the code work on Windows Server 2008, 2008 R2 and 2012