I'm building an application that will authenticate users via Active Directory. Nothing major, not adding or editing users, just validating credentials. I found many posts on this - some using System.DirectoryServices.AccountManagement (.NET 3.5 and above) or using LDAP DirectoryEntry...
I'm trying to figure out if I have to specify the AD server URL? When I test, I do not need to specify anything?
When is the AD server auto detected? When do I need to specify a URL?
Is there a call I can make to get the auto detected AD server URL?
When you create a domain
PrincipalContextorDirectoryContextwithout specifying a domain or path, the current context is used. i.e. the account the code is executing under. If the code is executing under an account not in a domain an exception will be thrown. This applies weather running in a windows forms app or in a service.To validate credentials against a domain all you need to do is:
However, this is not the best way to do this for many reasons. For example,
ValidateCredentialscan return true even if the user cannot log in due to expired passwords etc. Secondly, there is a much easier way to use active directory single sign-on for desktop applications:This returns the principal for the current user, there's no need to re-authenticate, windows already did that, and therefore you already know the user is valid.