ldap startls golang
To use StartTLS with LDAP in Golang, you can do the following:
- Import the necessary packages:
import (
"crypto/tls"
"gopkg.in/ldap.v3"
)
- Create a TLS configuration:
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: "ldap.example.com",
}
Note: Setting InsecureSkipVerify to true is not recommended for production use.
- 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()
- Bind to the LDAP server using
conn.StartTLS:
err = conn.StartTLS(tlsConfig)
if err != nil {
// handle error
}
- 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.
原文地址: https://www.cveoy.top/t/topic/bsrO 著作权归作者所有。请勿转载和采集!