To use StartTLS with LDAP in Golang, you can do the following:

  1. Import the necessary packages:
import (
    "crypto/tls"
    "gopkg.in/ldap.v3"
)
  1. Create a TLS configuration:
tlsConfig := &tls.Config{
    InsecureSkipVerify: true,
    ServerName:         "ldap.example.com",
}

Note: Setting InsecureSkipVerify to true is not recommended for production use.

  1. Connect to the LDAP server using ldap.DialTLS:
conn, err := ldap.DialTLS("tcp", "ldap.example.com:636", tlsConfig)
if err != nil {
    // handle error
}
defer conn.Close()
  1. Bind to the LDAP server using conn.StartTLS:
err = conn.StartTLS(tlsConfig)
if err != nil {
    // handle error
}
  1. Perform LDAP operations as usual:
// Search for entries
searchRequest := ldap.NewSearchRequest(
    "dc=example,dc=com",
    ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
    "(objectClass=inetOrgPerson)",
    []string{"dn", "cn", "mail"},
    nil,
)

sr, err := conn.Search(searchRequest)
if err != nil {
    // handle error
}

// Iterate through search results
for _, entry := range sr.Entries {
    dn := entry.DN
    cn := entry.GetAttributeValue("cn")
    mail := entry.GetAttributeValue("mail")
    // Do something with the results
}

Note: Replace ldap.example.com with the hostname of your LDAP server and dc=example,dc=com with the base DN of your LDAP directory.

ldap startls golang

原文地址: https://www.cveoy.top/t/topic/bsrO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录