LdapConnection Active Directory schema information for user object

1k Views Asked by At

I am looking for method or search filters which can help me to get schema attribute information of user object from active directory over SSL.

I am using LdapConnection class to connect to the server. It is easy to get authenticate server.

Here is code for authenticating:

public bool Authenticate(string password)
{
        try
        {
            var credential = new NetworkCredential(UserName, password, Domain);
            var ldapServer = Domain;
            var ldapConnection = new LdapConnection(ldapServer);
            ldapConnection.Bind(credential);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return false;
        }

        return false;
}

It returns a success result.

My requirements is to get all schema attribute of user object present in Active Directory using LdapConnection search request.

DirectoryEntry or PrincipalContext is easy to user get schema information but in my case I need all information over only LdapConnection class.

This is how I search user but this is method to get user information I need only schema information also this method returns only those attribute which has value.

LdapConnection connection = new LdapConnection(ldapServer);
connection.SessionOptions.SecureSocketLayer = true;
connection.SessionOptions.VerifyServerCertificate = (ldapConnection, certificate) => true;
connection.AuthType = AuthType.Negotiate;

NetworkCredential credential = new NetworkCredential(username, password);
connection.Credential = credential;
connection.Bind();

string filter = string.Format(CultureInfo.InvariantCulture, "(&(objectClass=user)(objectCategory=user) (sAMAccountName={0}))", LdapEncode(username));
var attributes = new[] { "sAMAccountName", "displayName", "mail" };

SearchRequest searchRequest = new SearchRequest(baseDn, filter, SearchScope.Subtree, attributes);

var searchResponse = (SearchResponse)connection.SendRequest(searchRequest);

if (searchResponse?.ResultCode == ResultCode.Success)
{
    var entry = searchResponse.Entries[0];
    var model = new LdapUserModel
                {
                    Identity = GetStringValue(entry, "sAMAccountName"),
                    Email = GetStringValue(entry, "mail"),
                    Username = GetStringValue(entry, "sAMAccountName"),
                };

    return model;
}
0

There are 0 best solutions below