0

I'm trying to check if the username/password for a remote computer, entered by a user on a WPF form are correct.

I have those strings: username, password and ip address.

I saw something about about "DirectoryEntry" but couldn't get it to work - the user is always authenticated even when the password is incorrect.

Any ideas?

Idanis
  • 1,918
  • 6
  • 38
  • 69
  • Are you talking about Active Directory authentication ? Because yes, there is a way to check whether a combination of username/password is is correct. – Dimitar Dimitrov Jan 20 '13 at 09:43
  • Yes, I am talking about Active Directory authentication. Could you please show me how? – Idanis Jan 20 '13 at 09:45
  • See if this answer helps - http://stackoverflow.com/questions/290548/validate-a-username-and-password-against-active-directory – Daniel Kelley Jan 20 '13 at 09:51
  • Should I use my IP as the domain? I use this: ` private bool Authenticate(string username, string password, string domain) { PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain); return (pc.ValidateCredentials(username, ServerPassword, ContextOptions.Negotiate)); }` but I get PrincipalServerDownException... – Idanis Jan 20 '13 at 10:00

1 Answers1

0

There are multiple ways, but the way I've done it before is like this (using DirectoryEntry), it goes like this:

string ldapConnectionString = @"LDAP://[domain_server]/CN=Users,DC=[domain]"

using (var de = new DirectoryEntry(
    ldapConnectionString, "username", "password", 
    AuthenticationTypes.Secure))
{
    return de.NativeObject != null; // if not null -> user is valid
}

Edit: What this code will do is, validate a combination of a username/password against active directory. I think I misunderstood you (if what you mean is, to see if a user CAN connect to a particular server -> as in HAS PERMISSION to, I'm not quite sure how to do that, or even if it's possible).

Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79
  • I get server is not operational exception. Am I writing the ldap string correctly: `string ldapConnectionString = @"LDAP://10.0.0.160/CN=Users,DC=[10.0.0.160]";`? – Idanis Jan 20 '13 at 10:06
  • I believe it should be something like this -> @"LDAP://10.0.0.160/CN=Users,DC=DOMAIN_NAME"; – Dimitar Dimitrov Jan 20 '13 at 10:10
  • Let's say you had the domain "COMPANY.US", you would go like this -> @"LDAP://10.0.0.160/CN=Users,DC=COMPANY, DC=US"; – Dimitar Dimitrov Jan 20 '13 at 10:12
  • Is 10.0.0.160 your domain controller or is it the server you're trying to connect ? (it should be the domain controller) – Dimitar Dimitrov Jan 20 '13 at 10:19
  • it's the server I'm trying to connect to. but then how do I know what's the domain controller, and how do choose this specific computer in the domain, out of all other computers? – Idanis Jan 20 '13 at 10:22