You need to have include reference to following two dlls:
OUTPUT:
=================================================
Group Name :: Administrators
Group Name :: Remote Desktop Users
Group Name :: Users
Group Name :: Guests
Group Name :: HelpServicesGroup
***************
User Name :: Administrator
User Name :: ASPNET
User Name :: Guest
User Name :: HelpAssistant
User Name :: IUSR_VIRTUAL01
User Name :: IWAM_VIRTUAL01
User Name :: SUPPORT_388945a0
User Name :: TestAdmin
=================================================
User Administrator is Member of Group Administrators.
User TestAdmin is Member of Group Administrators.
User Administrator is Member of Group Remote Desktop Users.
User INTERACTIVE is Member of Group Users.
User Authenticated Users is Member of Group Users.
User ASPNET is Member of Group Users.
User TestAdmin is Member of Group Users.
User Guest is Member of Group Guests.
User IUSR_VIRTUAL01 is Member of Group Guests.
User Guest is Member of Group Guests.
User IUSR_VIRTUAL01 is Member of Group Guests.
User SUPPORT_388945a0 is Member of Group HelpServicesGroup.
User Administrator is Member of Group Administrators.
User TestAdmin is Member of Group Administrators.
User INTERACTIVE is Member of Group Users.
User Authenticated Users is Member of Group Users.
User ASPNET is Member of Group Users.
User TestAdmin is Member of Group Users.
=================================================
Group Name :: Administrators
Group Name :: Backup Operators
Group Name :: Guests
Group Name :: Network Configuration Operators
Group Name :: Power Users
Group Name :: Remote Desktop Users
Group Name :: Replicator
Group Name :: Users
Group Name :: DB2ADMNS
Group Name :: DB2USERS
Group Name :: HelpLibraryUpdaters
Group Name :: HelpServicesGroup
Group Name :: SQLServer2005SQLBrowserUser$VIRTUAL01
Group Name :: SQLServerDTSUser$VIRTUAL01
Group Name :: SQLServerFDHostUser$virtual01$MSSQLSERVER
Group Name :: SQLServerMSASUser$VIRTUAL01$MSSQLSERVER
Group Name :: SQLServerMSSQLServerADHelperUser$VIRTUAL01
Group Name :: SQLServerMSSQLUser$virtual01$MSSQLSERVER
Group Name :: SQLServerMSSQLUser$virtual01$SQLEXPRESS
Group Name :: SQLServerReportServerUser$VIRTUAL01$MSRS10_50.MSSQLSERVER2008
Group Name :: SQLServerSQLAgentUser$VIRTUAL01$MSSQLSERVER
Group Name :: SQLServerSQLAgentUser$VIRTUAL01$SQLEXPRESS
***************
User Name :: Administrator
User Name :: ASPNET
User Name :: Guest
User Name :: HelpAssistant
User Name :: IUSR_VIRTUAL01
User Name :: IWAM_VIRTUAL01
User Name :: SUPPORT_388945a0
User Name :: TestAdmin
=================================================
Authenticate a Local User using UserName/Password pair:
OUTPUT:
User Name (TestAdmin) is a authenticated user!
or
User Name (TestAdmin) is a not authenticated, bad user name or password!
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
Adding reference for DLLs |
1: static void Main(string[] args)
2: {
3: List<string> usersList = new List<string>();
4: List<string> groupsList = new List<string>();
5:
6: // Populating Local Computer "Users" and "Groups"
7: Console.WriteLine("=================================================");
8:
9: //Create your domain context to Machine level
10: PrincipalContext pContext = new PrincipalContext(ContextType.Machine);
11:
12: //Query as UserPrincipal
13: UserPrincipal uPrincipal = new UserPrincipal(pContext);
14:
15: //Create principal searcher passing in the User principal
16: PrincipalSearcher pSearcher = new PrincipalSearcher(uPrincipal);
17:
18: // find all matches
19: foreach (Principal principal in pSearcher.FindAll())
20: {
21: if(!usersList.Contains(principal.Name))
22: usersList.Add(principal.Name);
23:
24: foreach (Principal grpPrincipal in principal.GetGroups())
25: {
26: if (!groupsList.Contains(grpPrincipal.Name))
27: groupsList.Add(grpPrincipal.Name);
28:
29: }
30: }
31:
32: //Groups
33: foreach(string group in groupsList )
34: Console.WriteLine(" Group Name :: " + group);
35: Console.WriteLine("***************");
36:
37: //Users
38: foreach (string user in usersList)
39: Console.WriteLine(" User Name :: " + user);
40:
41: Console.WriteLine("=================================================");
42: foreach (Principal principal in pSearcher.FindAll())
43: {
44:
45: foreach (Principal grpPrincipal in principal.GetGroups())
46: {
47: using (DirectoryEntry dGroupEntry = new DirectoryEntry(string.Format("WinNT://./{0},group", grpPrincipal.Name)))
48: {
49: foreach (Object member in (IEnumerable)dGroupEntry.Invoke("Members"))
50: {
51: using (DirectoryEntry dMemberEntry = new DirectoryEntry(member))
52: {
53: Console.WriteLine(string.Format(" User {0} is Member of Group {1}.", dMemberEntry.Name, grpPrincipal.Name));
54: }
55: }
56: }
57:
58: }
59: }
60:
61: Console.WriteLine("=================================================");
62: //clear lists
63: usersList.Clear();
64: groupsList.Clear();
65:
66: using (DirectoryEntry dEntry = new
67: DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"))
68: {
69: //Filter only "user" and "Groups" objects
70: dEntry.Children.SchemaFilter.Add("user");
71: dEntry.Children.SchemaFilter.Add("group");
72: foreach (DirectoryEntry dChildEntry in dEntry.Children)
73: {
74: if (dChildEntry.SchemaClassName.Equals("User", StringComparison.OrdinalIgnoreCase))
75: {
76: usersList.Add(dChildEntry.Name);
77: }
78: else
79: {
80: groupsList.Add(dChildEntry.Name);
81: }
82: }
83:
84: }
85:
86: //Groups
87: foreach (string group in groupsList)
88: Console.WriteLine(" Group Name :: " + group);
89: Console.WriteLine("***************");
90:
91: //Users
92: foreach (string user in usersList)
93: Console.WriteLine(" User Name :: " + user);
94: Console.WriteLine("=================================================");
95: Console.ReadKey();
96: }
OUTPUT:
=================================================
Group Name :: Administrators
Group Name :: Remote Desktop Users
Group Name :: Users
Group Name :: Guests
Group Name :: HelpServicesGroup
***************
User Name :: Administrator
User Name :: ASPNET
User Name :: Guest
User Name :: HelpAssistant
User Name :: IUSR_VIRTUAL01
User Name :: IWAM_VIRTUAL01
User Name :: SUPPORT_388945a0
User Name :: TestAdmin
=================================================
User Administrator is Member of Group Administrators.
User TestAdmin is Member of Group Administrators.
User Administrator is Member of Group Remote Desktop Users.
User INTERACTIVE is Member of Group Users.
User Authenticated Users is Member of Group Users.
User ASPNET is Member of Group Users.
User TestAdmin is Member of Group Users.
User Guest is Member of Group Guests.
User IUSR_VIRTUAL01 is Member of Group Guests.
User Guest is Member of Group Guests.
User IUSR_VIRTUAL01 is Member of Group Guests.
User SUPPORT_388945a0 is Member of Group HelpServicesGroup.
User Administrator is Member of Group Administrators.
User TestAdmin is Member of Group Administrators.
User INTERACTIVE is Member of Group Users.
User Authenticated Users is Member of Group Users.
User ASPNET is Member of Group Users.
User TestAdmin is Member of Group Users.
=================================================
Group Name :: Administrators
Group Name :: Backup Operators
Group Name :: Guests
Group Name :: Network Configuration Operators
Group Name :: Power Users
Group Name :: Remote Desktop Users
Group Name :: Replicator
Group Name :: Users
Group Name :: DB2ADMNS
Group Name :: DB2USERS
Group Name :: HelpLibraryUpdaters
Group Name :: HelpServicesGroup
Group Name :: SQLServer2005SQLBrowserUser$VIRTUAL01
Group Name :: SQLServerDTSUser$VIRTUAL01
Group Name :: SQLServerFDHostUser$virtual01$MSSQLSERVER
Group Name :: SQLServerMSASUser$VIRTUAL01$MSSQLSERVER
Group Name :: SQLServerMSSQLServerADHelperUser$VIRTUAL01
Group Name :: SQLServerMSSQLUser$virtual01$MSSQLSERVER
Group Name :: SQLServerMSSQLUser$virtual01$SQLEXPRESS
Group Name :: SQLServerReportServerUser$VIRTUAL01$MSRS10_50.MSSQLSERVER2008
Group Name :: SQLServerSQLAgentUser$VIRTUAL01$MSSQLSERVER
Group Name :: SQLServerSQLAgentUser$VIRTUAL01$SQLEXPRESS
***************
User Name :: Administrator
User Name :: ASPNET
User Name :: Guest
User Name :: HelpAssistant
User Name :: IUSR_VIRTUAL01
User Name :: IWAM_VIRTUAL01
User Name :: SUPPORT_388945a0
User Name :: TestAdmin
=================================================
Authenticate a Local User using UserName/Password pair:
1:
2: static void Main(string[] args)
3: {
4: string userName = "TestAdmin";
5: string password = "********";
6:
7: //Authenticate a Local User using UserName/Password pair
8: using (PrincipalContext context = new PrincipalContext(ContextType.Machine))
9: {
10: bool isAuthenticated = context.ValidateCredentials(userName, password);
11: if (isAuthenticated)
12: {
13: Console.WriteLine(string.Format("User Name ({0}) is a authenticated user!", userName));
14: }
15: else
16: {
17: Console.WriteLine(string.Format("User Name ({0}) is a not authenticated, bad user name or password!", userName));
18: }
19: }
20:
21: Console.ReadKey();
22: }
OUTPUT:
User Name (TestAdmin) is a authenticated user!
or
User Name (TestAdmin) is a not authenticated, bad user name or password!