c# - 通过 userCertificate 属性查询 LDAP,带有证书

标签 c# ldap adlds

我正在获取 byte[] 的证书,例如:

byte[] certRaw;
X509certificate2 x509Cert = new x509Certificate2(Request.ClientCertificate.Certificate);
certRaw = x509Cert.GetRawCertData();

然后我尝试通过该值在 LDAP 中查找用户。

DirectorySearcher finduser = new DirectorySearcher(ldapconnection);
findUser.Filter = "(&(objectClass=user)(userCertificate=" + certRaw + "))";

这无法匹配 LDAP 中的 userCertificate。 如果我从证书中获取该用户并使用它而不是 userCertificate 属性,我就可以通过 CN 查找用户,但这不是我得到的要求。感谢您的帮助。

最佳答案

有人问我同样的事情!

好吧,我被问到“当我只有证书时,如何通过针对他们发布的证书在 Active Directory 中找到用户,而证书中不一定包含用户的主题名称,也可能不是与任何事物一致”。所以:足够接近了。

我把你的问题和另一个答案放在一起,得到了一些看起来可行的东西。

通常的免责声明适用:不要这样做...这可能会削弱您的 DC,或者有轻微的致癌性,或其他什么。不知道它如何或是否适用于填充到 userCertificate 中的多个证书,因此在这种情况下可能需要修改查询。

要运行它,您需要一个您正在搜索的证书的 .CER 文件。然后是

FindUserWithCert mycert.CER 

然后你离开了。

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.DirectoryServices;

/// This is sample code only so please enjoy it with all care
/// and no responsibility :) 
/// 
namespace FindUserWithCert
{
    class Program

    {
        static void Main(string[] args)
        {
            byte[] certRaw; 
            X509Certificate2 x509Cert = new X509Certificate2(args[0]); 
            certRaw = x509Cert.GetRawCertData();

            string certBytes = "";

            foreach (byte b in certRaw){
                certBytes += String.Format("\\{0:X}", b);
                //Console.Write (String.Format("\\{0:X}",b));
            }

            DirectorySearcher findUser = new DirectorySearcher("(&(objectClass=user)(userCertificate=" + certBytes + "))");

            foreach (System.DirectoryServices.SearchResult thing in findUser.FindAll())
            {
                Console.WriteLine(thing.Path.ToString());
            }

        }
    }
}

关于c# - 通过 userCertificate 属性查询 LDAP,带有证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25516019/

相关文章:

c# - Return 后我应该在函数内调用 Dispose() 吗?

c# - 从远程计算机简单绑定(bind)到 AD-LDS 失败

c# - 使用 UserPrincipal 类创建新的 AD-LDS 用户总是失败

regex - 使用正则表达式从 LDAP 路径中提取名称

.net - 使用 “SQL”查询Active Directory?

c# - ADLDS SSL 证书颁发身份验证失败

c# - NHibernate 加入子类错误 - 对象与目标类型不匹配

c# - 根据字符串生成唯一的哈希码

c# - 删除特殊字符并排除一个词

c - ldap_search_ext 返回额外结果