C# 从AD服务器检索X509证书

C# 从AD服务器检索X509证书,c#,encryption,certificate,x509certificate,x509certificate2,C#,Encryption,Certificate,X509certificate,X509certificate2,我们是否可以使用c#从AD服务器获取X509公共证书以加密电子邮件。 现在我正在使用本地存储来获取证书和加密邮件 static public X509Certificate2 GetRecipientCertPublic(string recipientName) { X509Store storeAddressBook = new X509Store(StoreName.AddressBook, StoreLocation.CurrentUser); sto

我们是否可以使用c#从AD服务器获取X509公共证书以加密电子邮件。 现在我正在使用本地存储来获取证书和加密邮件

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中的行为是不同的。即使接收方的公共证书不在本地计算机证书管理器中。它可以从组织的centeral服务器或广告服务器(我不是很确定)获取公共证书并发送加密邮件。

嗨,布莱尔,谢谢你的回答,我一年前就发布了这个问题。现在,当我在网上搜索同一个问题时,它重定向到了我发布的同一个问题。:-)嗨,布莱尔,我们是否也可以下载私人证书。嗨,布莱尔,谢谢你的回答,我一年前就发布了这个问题。现在,当我在网上搜索同一个问题时,它重定向到了我发布的同一个问题。:-)嗨,布莱尔,我们是否也可以下载私人证书。
// 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);
    }
}