简体   繁体   English

如何正确处理使用ADODB ADsDSObject提供程序为Ldap搜索创建的对象

[英]How to properly dispose objects created for Ldap search using ADODB ADsDSObject provider

I am looking for the best way how to lookup LDAP directory for users by given criteria. 我正在寻找最佳方法,如何根据给定条件为用户查找LDAP目录。 At the moment the best performance seems to offer usage of ADsDSObject provider. 目前,最好的性能似乎是使用ADsDSObject提供程序。 The code will run in ASP.NET web site. 该代码将在ASP.NET网站中运行。

I would like to confirm how to properly dispose the resources. 我想确认如何正确配置资源。 Here is the code used at the moment. 这是目前使用的代码。 Is the code releasing resources correctly or need to be improved? 代码是否正确释放资源或需要改进?

public static List<LookupValues> FindBy(LdapSearchCriteria criteria)
{
    List<LookupValues> usersMatchingCriteria = new List<LookupValues>();

    ADODB.Command adoCommand = new ADODB.Command();
    ADODB.Connection adoConnection = new ADODB.Connection();
    ADODB.Recordset adoResultSet = new ADODB.Recordset();

    adoConnection.ConnectionString = connectionString;
    adoConnection.Open();
    adoCommand.ActiveConnection = adoConnection;
    adoCommand.CommandText = BuildSelectStatmentFrom(criteria);
    object dummy = Type.Missing;

    try
    {
        adoResultSet = adoCommand.Execute(out dummy, ref dummy, 0);
        if (adoResultSet != null)
        {
            while (adoResultSet.EOF == false)
            {
                LookupValues value = new LookupValues();
                for (int i = 0; i < adoResultSet.Fields.Count; i++)
                {
                    switch (adoResultSet.Fields[i].Name)
                    {
                        case "a-foreignGivenName":
                            value.FirstName = (adoResultSet.Fields[i].Value).ToString();
                            break;
                        case "a-foreignSn":
                            value.LastName = (adoResultSet.Fields[i].Value).ToString();
                            break;
                    }
                }
                usersMatchingCriteria.Add(value);
                adoResultSet.MoveNext();
            }
        }
    }
    finally
    {
        if (adoResultSet != null)
        {
            adoResultSet.Close();
            adoResultSet = null;
        }

        if (adoConnection != null)
        {
            adoConnection.Close();
            adoConnection = null;
        }
    }

    return usersMatchingCriteria;
}

I found equivalent and even a bit faster to use classes from System.DirectoryServices.Protocols namespace. 我发现使用System.DirectoryServices.Protocols命名空间中的类等效,甚至更快。 Equivalent code using .NET classes 使用.NET类的等效代码

public List<LookupValues> FindBy(LdapSearchCriteria criteria)
{
    List<LookupValues> usersMatchingCriteria = new List<LookupValues>();

    NetworkCredential credentials = new NetworkCredential(connectionDetails.UserName, connectionDetails.Password, connectionDetails.Domain);
    LdapDirectoryIdentifier directoryIdentifier = new LdapDirectoryIdentifier(connectionDetails.Server, connectionDetails.Port, false, false);

    using (LdapConnection connection = CreateConnection(directoryIdentifier))
    {
        connection.Bind(credentials);

        SearchRequest search = CreateSearchRequest(criteria);

        SearchResponse response = connection.SendRequest(search) as SearchResponse;
        foreach (SearchResultEntry entry in response.Entries)
        {
            LookupValues foundUser = GetUserDetailsFrom(entry);
            usersMatchingCriteria.Add(foundUser);
        }
    }

    return usersMatchingCriteria;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM