Detecting if the user is in a group
Using UserPrincipal.GetAuthorizationGroups check if the user is in a group that is allowed administrative access to the machine.
First get the UserPrincipal object using FindByIdentity. Then get the authorization groups the user is a member of. Check each group to see if it matches the built-in Administrators group. If the built-in Administrators group is not part of the user authorization groups, the user is not an administrator on the local machine.
using System.DirectoryServices.AccountManagement;
using System.Linq;
var name = Environment.UserName;
var user = UserPrincipal.FindByIdentity( new PrincipalContext( ContextType.Domain ), name );
var groups = user.GetAuthorizationGroups();
var isAdmin = groups.Any( g => g.Name == "Administrators" );
Console.WriteLine( "Admin: " + isAdmin );
a source to share
You can use System.DirectoryServices to boot the local computer for the first time and then search for any users in the given group. Try using the following code:
DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
DirectoryEntry admGroup = localMachine.Children.Find("administrators","group");
object members = admGroup.Invoke("members", null);
Then create a new DirectoryEntry for each member of the member object:
foreach (object groupMember in (IEnumerable)members)
{
DirectoryEntry member = new DirectoryEntry(groupMember);
//Do what you want
}
The member object inside this foreach loop has a load of user data inside it. Compare your member name with the current one in the loop:
if (memberSearch.name == member.name) {
return true;
} else {
return false;
}
You can also search for a member object to find the user, etc. There are many ways to do this. Hope this helps!
a source to share