简体   繁体   中英

Authenticate user from specific groups on LDAP server using C#

I have a few nested groups on LDAP server and users in these groups. How can I authenticate users with given username and password by searching only in groups(not whole domain)? Does bind do this?

As confirmed by you in the comment section of this question, the LDAP server you're talking about is an Active Directory server. So, my answer is based on this famous answer about how to validate a username and password against Active Directory , except that I've made a modification based on your requirement to limit the scope of search.

If you work on .NET 3.5 or newer, you can use the System.DirectoryServices.AccountManagement namespace's PrincipalContext Constructor (ContextType, String, String) and easily verify your credentials:

// create a "principal context"
using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOUR.DOMAIN",
             "OU=Where,OU=You,OU=Wanna,OU=Search,DC=YOUR,DC=DOMAIN"))
   // change your container to a base OU where all your users are located.
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM