Here is how to check if a user has a valid active directory user name and password:-
Add the following directives:
using System.DirectoryServices;
using System.Security.Principal;
and the following reference:-
System.DirectoryServices
Here is the function:-
public static bool LogonValid(string userName, string password, string domain)
{
DirectoryEntry de = new DirectoryEntry(null, domain + "\\" + userName, password);
try
{
object o = de.NativeObject;
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "samaccountname=" + userName;
ds.PropertiesToLoad.Add("cn");
SearchResult sr = ds.FindOne();
if(sr == null) throw new Exception();
return true;
}
catch
{
return false;
}
}
Usage Example:-
If(LogonValid(“r_dargan”, "Password",“DomainName”))
{
…
}
If anyone has a better method of doing this, then let me know!
No comments:
Post a comment