Connect to Active Directory via LDAP

前端 未结 3 1250
花落未央
花落未央 2020-11-28 22:43

I want to connect to our local Active Directory with C#.

I\'ve found this good documentation.

But I really don\'t get how to connect via LDAP.

Can so

3条回答
  •  暖寄归人
    2020-11-28 23:01

    If your email address is 'myname@mydomain.com', try changing the createDirectoryEntry() as below.

    XYZ is an optional parameter if it exists in mydomain directory

    static DirectoryEntry createDirectoryEntry()
    {
        // create and return new LDAP connection with desired settings
        DirectoryEntry ldapConnection = new DirectoryEntry("myname.mydomain.com");
        ldapConnection.Path = "LDAP://OU=Users, OU=XYZ,DC=mydomain,DC=com";
        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
        return ldapConnection;
    }
    

    This will basically check for com -> mydomain -> XYZ -> Users -> abcd

    The main function looks as below:

    try
    {
        username = "Firstname LastName"
        DirectoryEntry myLdapConnection = createDirectoryEntry();
        DirectorySearcher search = new DirectorySearcher(myLdapConnection);
        search.Filter = "(cn=" + username + ")";
        ....    
    

提交回复
热议问题