Vij123
Vij123

Reputation: 47

How can I query Active Directory in .NET Core?

I want to query Active Directory in a .NET Core project and get details of a user by providing the user id.

LDAP server path is in this format: ldap://

Upvotes: 1

Views: 9673

Answers (1)

Jaime
Jaime

Reputation: 6091

The LDAP URLs specifies the server you want to use and the query to perform. For instance, in the query:

ldap://ds.example.com:389/dc=example,dc=com?givenName,sn,cn?sub?(uid=john.doe) 
  • ds.example.com:389 comprises the LDAP server and port.
  • dc=example,dc=com is the base DN
  • givenName,sn,cn are the attributes to include in the result,
  • sub is the scope, and
  • (uid=john.doe) is the query

There is an LDAP library in .NET, that is not included in .NET Core. You may use the Novell LDAP libraries to query an LDAP and/or Active Directory Server in .NET Core. The documentation is available at Novell.

The documentation for the Novell library mentions the LDAP URLs. If you have LDAP URLs, I think you must split the URL to obtain each part and perform the query.


Getting information of a user

Using the Novell library, you can can connect to the server and perform LDAP queries using attributes of the entries such as in queries like (givenName=John), (!givenName=John) or (givenName=Jo*). You may check the example code here, here and here.

In Windows / Active Directory, the login name is stored in sAMAccountName. You can use a query like

(&(objectClass=user)(objectClass=person)(sAMAccountName={0}))

Upvotes: 5

Related Questions