现在的位置: 首页 > 综合 > 正文

C#读取本地CA证书并绑定到List

2013年03月12日 ⁄ 综合 ⁄ 共 1051字 ⁄ 字号 评论关闭

----------------------------读取本地CA证书并绑定---------------
    protected void Bind_CA()
    {
        string mail = null;
        string realname = null;
        X509Store store = new X509Store(StoreName.My,StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly|OpenFlags.OpenExistingOnly);
        X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
        X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByIssuerName, "Foxconn CA", false);
        foreach (X509Certificate2 x509 in fcollection)
        {
        
            string[] dn = x509.Subject.Split(',');
            foreach (string str in dn)
            {
                 if (str.Trim().StartsWith("E"))
                {
                    mail = str.Trim().Substring(str.Trim().IndexOf("=") + 1);
                 } 

                else if (str.Trim().StartsWith("CN"))
                {
                    realname = str.Trim().Substring(str.Trim().IndexOf("=") + 1);
                 }
            }
            CAList.Items.Add(new ListItem(realname, mail));
        }
    }

抱歉!评论已关闭.