正在从AD Server检索X509证书

本文关键字:检索 X509 证书 Server AD | 更新日期: 2023-09-27 18:28:27

有没有任何方法可以使用c#从AD Server中获取X509 Public Cetrificates以加密电子邮件。现在我正在使用本地存储来提取证书和加密邮件。

static public X509Certificate2 GetRecipientCertPublic(string recipientName)
{  
    X509Store storeAddressBook =
        new X509Store(StoreName.AddressBook, StoreLocation.CurrentUser);
    storeAddressBook.Open(OpenFlags.ReadOnly);
    X509Certificate2Collection certColl =
        storeAddressBook.Certificates.Find(X509FindType.FindBySubjectName, recipientName, false);
    storeAddressBook.Close();
    if (certColl.Count != 0)
    {
        return certColl[0];
    }
    else
    {
        return null;
    }
}

正如我所看到的,Outlook中的行为是不同的。即使接收方的公共证书不存在于本地机器证书管理器中。它能够从组织的中心服务器或广告服务器(我对此不太确定)获取公共证书并发送加密邮件。

正在从AD Server检索X509证书

// Where ##### is the name of your AD server
DirectoryEntry de = new DirectoryEntry("LDAP://#####");
DirectorySearcher dsearch = new DirectorySearcher(de);
//Search how you want.  Google "LDAP Filter" for more.
dsearch.Filter = "(cn=#####)"; 
SearchResultCollection rc = dsearch.FindAll();
X509Certificate stt = new X509Certificate();
foreach (SearchResult r in rc)
{
    if (r.Properties.Contains("userCertificate"))
    {
        // This is hard coded to the first element.
        // Some users may have multiples.  Use ADSI Edit to find out more.
        Byte[] b = (Byte[])r.Properties["userCertificate"][0];
        X509Certificate cert1 = new X509Certificate(b);
    }
}