通过LDAP查询来自Active Directory的IP地址的用户信息?

In Go application client make HTTP request. I know the IP address of the client who make the request.

Is it possibly to know user information (username, email, e.t.c) by IP address from Active Directory by LDAP query? What kind of filter I need to use?

l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
if err != nil {
    log.Fatal(err)
}
defer l.Close()

searchRequest := ldap.NewSearchRequest(
    "dc=example,dc=com", // The base dn to search
    ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
    "(&(objectClass=organizationalPerson))", // The filter to apply
    []string{"dn", "cn"},                    // A list attributes to retrieve
    nil,
)

sr, err := l.Search(searchRequest)
if err != nil {
    log.Fatal(err)
}

for _, entry := range sr.Entries {
    fmt.Printf("%s: %v
", entry.DN, entry.GetAttributeValue("cn"))
}

Active Directory does not store the IP or computer name that each person uses. Trying to match a user with a computer is difficult because a person can usually log in from any computer.

If you really want that information, there are a couple ways:

  1. If you have admin rights to each computer in your environment, you could access \\IPAddress\c$\Users and check which profile was most-recently used.
  2. Make people authenticate to your website using Windows Authentication. As long as your website is in the Trusted Sites (in Internet Options) people will be automatically logged in. I have no experience doing this with Go, but here is an implementation I found on Google: https://gowalker.org/github.com/mfcollins3/windowsauthtoken The idea is that your website will run behind IIS. IIS performs the authentication and just forwards the user information to your application.