I have a username and password for active directory and I want login to active directory using C#. How I can do this in Windows Forms?
-
What do you want to achieve exactly? Login - then what? – abatishchev Jul 26 '12 at 08:24
-
Call Some `WCF` services that has `WS-Http Binding` – Arian Jul 26 '12 at 08:25
-
So do you want to login into Active Directory or into Windows Server which is into a domain? – abatishchev Jul 26 '12 at 08:26
-
I want to login In Active Directory – Arian Jul 26 '12 at 08:30
-
You keep repeating that - elaborate! What is it you want to do? What do you want to _accomplish_? – J. Steen Jul 26 '12 at 08:40
-
Does it possible to login to AD using `NetworkCredential`? – Arian Jul 26 '12 at 08:42
4 Answers
This Question is from a couple of years ago, I hope this answer can help people in the future. This is working for me:
Add these references:
- using System.DirectoryServices;
- using System.DirectoryServices.AccountManagement;
After that, you can use this code in your app:
PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOUR DOMAIN");
bool Valid = pc.ValidateCredentials("User", "password");
The variable called: Valid, will show you a True value if the logIn is Ok.
For more information you can visit this page. The page is from here, stackOverFlow, and it will show you a lot of information about: "login with MS Active Directory"
- 1
- 1
- 3,481
- 1
- 34
- 44
Solution
Connecting to an Active Directory is very easy.
You must use the DirectoryEntry object (in the namespace System.DirectoryServices).
The constructor for this object takes three strings in parameters:
- the path to the Active Directory. This path has the format:
LDAP://your-name-AD - the username for the connection
- the corresponding password
Example
using System.DirectoryServices;
try
{
DirectoryEntry Ldap = new DirectoryEntry("LDAP://your-name-AD", "Login", "Password");
}
catch(Exception Ex)
{
Console.WriteLine(Ex.Message);
}
-
what is `DirectoryEntry`? I want to user `using System.Security.Principal;` thanks – Arian Jul 26 '12 at 08:26
-
What is difference between your solution and `NetworkCredential cred = new NetworkCredential("User", "Pass", "Domain");`? can you explain more please – Arian Jul 26 '12 at 08:37
-
[`DirectoryEntry`](http://msdn.microsoft.com/en-us//library/system.directoryservices.directoryentry(v=vs.80).aspx) handles all actions performed on an AD ; not [`NetworkCredential`](http://msdn.microsoft.com/en-us//library/system.net.networkcredential(v=vs.80).aspx). – GG. Jul 26 '12 at 08:56
There you go. This method validates username/password against Active Directory, and has been a part of my toolbox of functions for quite some time.
//NOTE: This can be made static with no modifications
public bool ActiveDirectoryAuthenticate(string username, string password)
{
bool result = false;
using (DirectoryEntry _entry = new DirectoryEntry())
{
_entry.Username = username;
_entry.Password = password;
DirectorySearcher _searcher = new DirectorySearcher(_entry);
_searcher.Filter = "(objectclass=user)";
try
{
SearchResult _sr = _searcher.FindOne();
string _name = _sr.Properties["displayname"][0].ToString();
result = true;
}
catch
{ /* Error handling omitted to keep code short: remember to handle exceptions !*/ }
}
return result; //true = user authenticated!
}
The software executing this must be run on a computer inside the domain, obviously (or you would have no Active Directory to authenticate your credentials against).
- 23,004
- 4
- 39
- 73
-
What is difference between your solution and NetworkCredential cred = new NetworkCredential("User", "Pass", "Domain");? can you explain more please – Arian Jul 26 '12 at 08:51
-
NetworkCredential is just an object with some data inside, it doesn't validate anything. – Alex Jul 26 '12 at 09:01
a shorter answer is to add the reference System.DirectoryServices.AccountManagement
Then use UserPrincipal.Current.Context.ValidateCredentials("username", "password");
But i guess you will need to be joined to the domain you want to validate for.
- 11
- 1
-
Thanks for the answer! But I encountered an DirectoryOperationException with the code above, and I solved it by adding the third parameter "ContextOptions.Negotiate". – HCH Mar 23 '22 at 08:54