i"ve written code for Active directory LDAP user's authentication . It authenticates all users account in AD, But i want only Administrator account authentication not other user account(see below code). And also find domain name of connecting DNS(refer attached image).
try
{
DirectoryEntry entry = new DirectoryEntry(Domain, UserName, Password);
object nativeObject = entry.NativeObject;
Program.fileWrite.WriteLine(DateTime.Now + "\t Login with credentials " + UserName + " and " + Password);
return true;
}
catch (DirectoryServicesCOMException e)
{
Program.fileWrite.WriteLine(DateTime.Now + "\t " + e.Message);
return false;
}
Try this code :
public static bool ValidateCredential(string domain, string userName, string password)
{
using (var context = new PrincipalContext(ContextType.Domain, domain))
{
using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName))
{
if (user == null) return false;
using (var group = GroupPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "Domain Admins"))
{
if (group == null) return false;
foreach (var member in group.GetMembers())
{
if (member.Sid.Equals(user.Sid))
{
return context.ValidateCredentials(userName, password);
}
}
}
}
}
return false;
}