S
Steffen Balslev
I tried to find a way to validate user credentials using C#, searching google and lots of other news and kb sites left me without a solution.
You can use a SSPI but it's that easy to implement so I found a simple way and here it is:
using System.DirectoryServices;
public bool Win2kCredentialsIsValid(string domain, string username, string password)
{
bool validLogin = false;
string adPath = "LDAP://" + domain + "/rootDSE";
DirectoryEntry adRoot = new DirecotryEntry(adPath, domain + "\\" + username, password, AuthenticationTypes.ReadonlyServer);
try
{
object o = adRoot.Properties["defaultNamingContext"]
}
catch
{
return false;
}
return true;
}
Calling the function will return true if the credentials are valid otherwise false.
Example: bool isValid = Win2kCredentialsIsValid("mydomain", "myuser", "mypassword");
I found if you do not use "domain\username" in the username parameter of the DirectoryEntry constructor you will only be able to validate local user accounts. This means if machine you are testing on is a Directory Server you will only be able to validate the administrator username and password.
So the function can only validate domain credentials with is what i need
I hope some of you can use this
Regards
Steffen Balslev
You can use a SSPI but it's that easy to implement so I found a simple way and here it is:
using System.DirectoryServices;
public bool Win2kCredentialsIsValid(string domain, string username, string password)
{
bool validLogin = false;
string adPath = "LDAP://" + domain + "/rootDSE";
DirectoryEntry adRoot = new DirecotryEntry(adPath, domain + "\\" + username, password, AuthenticationTypes.ReadonlyServer);
try
{
object o = adRoot.Properties["defaultNamingContext"]
}
catch
{
return false;
}
return true;
}
Calling the function will return true if the credentials are valid otherwise false.
Example: bool isValid = Win2kCredentialsIsValid("mydomain", "myuser", "mypassword");
I found if you do not use "domain\username" in the username parameter of the DirectoryEntry constructor you will only be able to validate local user accounts. This means if machine you are testing on is a Directory Server you will only be able to validate the administrator username and password.
So the function can only validate domain credentials with is what i need

I hope some of you can use this

Regards
Steffen Balslev