I'm having problems getting groups from Active Directory through System.DirectoryServices
Initially, I ran the application on a computer that was registered in a domain, but since it was a living domain, I did not want to make any entries in AD, which was the case, so I configured the Windows XP machine as the host operating system and installed server Windows 2003 on a virtual machine.
I added another Ethernet port to the computer and set the switch, 1 Ethernet port is for the virtual machine and the other is for the host.
After setting up the IP addresses to transfer them, I passed the application to the host machine and ran it, but I got a DirectoryServicesCOMException .
With the message that the username and password were invalid :( just to verify that this is not an active directory, I created a third virtual machine and installed Windows XP, which I added to the domain with the credentials verified in APP, it works.
Therefore, I thought that this should be because the machine on which the application is running is not part of the domain.
Here is the code block that caused the problem:
public CredentialValidation(String Domain, String Username, String Password, Boolean Secure) { //Validate the Domain! try { PrincipalContext Context = new PrincipalContext(ContextType.Domain, Domain); //Throws Exception _IsValidDomain = true; //Test the user login _IsValidLogin = Context.ValidateCredentials(Username, Password); //Check the Group Admin is within this user //******HERE var Results = UserPrincipal.FindByIdentity(Context, Username).GetGroups(Context); foreach(Principal Result in Results) { if (Result.SamAccountName == "Domain Admins") { _IsAdminGroup = true; break; } } Results.Dispose(); Context.Dispose(); } catch (PrincipalServerDownException) { _IsValidDomain = false; } }
The information in the login dialog box is entered as follows:
Domain: test.internal Username: testaccount Password: Password01
Hope someone can shed some light on this mistake.
Update:
After checking the security logs on the server, I see that my login attempts were successful, but this happens before:
_IsValidLogin = Context.ValidateCredentials(Username, Password);
The line after im checks the groups causes an error, so the main problem is that the lines of code below do not work correctly from a computer that is not connected to the network:
var Results = UserPrincipal.FindByIdentity(Context, Username).GetGroups(Context);