运行GetTypes()方法时,类型变量始终返回null

时间:2019-02-13 16:18:42

标签: c# winforms active-directory

我一直遇到一个令人沮丧的问题。我试图根据Active Directory对用户进行身份验证,为此,我将用户变量传递给以下类。

        public static ILdapAuthentication CreateInstance(string domainAndUser, string password, string ldapPath)
    {
        string[] dllPaths = Directory.GetFiles(ExecutingAssemblyDirectory, "*.dll");

        List<Assembly> listOfAssemblies = new List<Assembly>();

        foreach (var dllPath in dllPaths.Where(x => x.Contains("ActiveDirectoryAuthentication")))
        {
            Assembly assembly = Assembly.LoadFrom(dllPath);
            listOfAssemblies.Add(assembly);
        }

        Type type = null;

        int foundTypes = 0;
        foreach (var assembly in listOfAssemblies)
        {
            type =
               assembly.GetTypes()
                   .FirstOrDefault(x => x.GetInterfaces().Any(i => i == typeof(ILdapAuthentication)));

            if (type == null)
                continue;

            foundTypes++;
        }

        if (foundTypes == 0)
            throw new Exception("ActiveDirectoryAuthentication DLL not found.");

        if (foundTypes > 1)
            throw new Exception("Only one ActiveDirectoryAuthentication DLL must be used.");

        return Activator.CreateInstance(type, domainAndUser, password, ldapPath) as ILdapAuthentication;
    }

该问题发生在foreach循环中,因为我尝试获取我的Types,它始终返回null,甚至没有命中下面的Interface(ILDPAuthentication)代码。

    public interface ILdapAuthentication
{
    bool IsActiveDirectoryUserValid();
}

它将调用以下代码:

    public class LdapAuthentication : ILdapAuthentication
{
    private string DomainAndUser { get; set; }
    private string Password { get; set; }
    private string LdapPath { get; set; }

    public LdapAuthentication(string domainAndUser, string password, string ldapPath)
    {
        this.DomainAndUser = domainAndUser;
        this.Password = password;
        this.LdapPath = ldapPath;
    }

    public bool IsActiveDirectoryUserValid()
    {
        try
        {
            if (!this.DomainAndUser.Contains('\\'))
                throw new Exception("Domain User is invalid.");

            string[] userLogin = this.DomainAndUser.Split('\\');
            string domain = userLogin[0];
            string userName = userLogin[1];

            DirectoryEntry entry = new DirectoryEntry(this.LdapPath, this.DomainAndUser, this.Password);
            object obj = entry.NativeObject;

            DirectorySearcher search = new DirectorySearcher(entry);

            search.Filter = "(SAMAccountName=" + userName + ")";
            search.PropertiesToLoad.Add("CN");

            SearchResult result = search.FindOne();

            if (null == result)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            throw;
        }
    }
}

初始类在我复制到其中的名为ActiveDirectoryAuthentication的应用程序文件夹中查找DLL。

1 个答案:

答案 0 :(得分:1)

我以前见过-显式加载的程序集中的类型与引用的项目中的类型不匹配。因为您正在使用:

i == typeof(ILdapAuthentication)

您依赖于Type类的相等性比较,当您期望它时,它可能不会返回相等性。我建议您改为:

i.FullName == typeof(ILdapAuthentication).FullName

将使用简单的字符串比较。