We help IT Professionals succeed at work.

User ID and Password Authentication with Microsoft Windows 7 Active Directory

How can I authenticate the User ID and Password to be valid from VB.Net and VB6 programs with Windows 7 Active Directory?
Comment
Watch Question

CERTIFIED EXPERT

Commented:
System.DirectoryServices.AccountManagement
https://docs.microsoft.com/en-us/dotnet/api/system.directoryservices.accountmanagement?view=netframework-4.8
NET 3.5 or newer
Imports System.DirectoryServices.AccountManagement

 'This example does require a reference to System.DirectoryServices.AccountManagement as well

 ' create a "principal context" - e.g. your domain (could be machine, too)
 Dim pc As New PrincipalContext(ContextType.Domain, "YOURDOMAIN")
 Dim isvalid As Boolean = pc.ValidateCredentials("myuser", "mypassword")

Open in new window

Senior Consultant
CERTIFIED EXPERT
Awarded 2017
Distinguished Expert 2019
Commented:
This is mine that includes group
public static bool GetIsAuthenticated_Static(string fqdn, string domain, string group, string user, string password)
{
    //Blank user of group, don't event try
    if (fqdn == "" || domain == "" || group == "" || user == "" || password == "")
    {
        //Failed
        return false;
    }
    
    try
    {
        PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, fqdn);
    
        //First validate account. This should prevent account lockouts
        if (principalContext.ValidateCredentials(user, password))
        {
            //Connect to domain with credentials
            PrincipalContext domainCtx = new PrincipalContext(ContextType.Domain, fqdn, user, password);
    
            //Connect to user
            UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domainCtx, user);
    
            //Protect against group not exist
            if (GroupPrincipal.FindByIdentity(domainCtx, group) != null)
            {
                //Check if user is part of group
                if (userPrincipal.IsMemberOf(GroupPrincipal.FindByIdentity(domainCtx, group)))
                {
                    //Success
                    return true;
                }
            }
        }
    }
    catch
    {
    }
    
    //Failed
    return false;
}

Open in new window

Author

Commented:
Thanks Hilltop and Shaun!  I'll try it in the next day.

Author

Commented:
Thank you very much!
Shaun VermaakSenior Consultant
CERTIFIED EXPERT
Awarded 2017
Distinguished Expert 2019

Commented:
Anytime ;)