Monday, 13 June 2005

Verify a users password (written for .net 1.1)

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:-

Here is the function:-
public static bool LogonValid(string userName, string password, string domain)
DirectoryEntry de = new DirectoryEntry(null, domain + "\\" + userName, password);
object o = de.NativeObject;

DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "samaccountname=" + userName;
SearchResult sr = ds.FindOne();
if(sr == null) throw new Exception();
return true;
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: