C#向ldap导入证书

来源:互联网 发布:java abstract class 编辑:程序博客网 时间:2024/06/05 11:59

1.将证书文件使用流读入,存放在byte[]中

                FileStream fs = new FileStream("c://cert//client.der", FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                byte[] Certbyte = br.ReadBytes((int)fs.Length);
                fs.Close();

2.将byte[]转为sbyte[] CertSbyte(详见上篇文章)

3.使用novell的ldap-c#类库,将带有证书的条目导入ldap

               LdapAttributeSet attributeSet = new LdapAttributeSet();
              attributeSet.Add(new LdapAttribute( "objectclass", "inetOrgPerson"));
              attributeSet.Add(new LdapAttribute("cn", new string[] { "James Smith", "Jim Smith", "Jimmy mith" }));     

                attributeSet.Add(new LdapAttribute("userCertificate;Binary",CertSbyte));

                string dn = "cn=Jim Smith," + containerName;
                LdapEntry newEntry = new LdapEntry(dn, attributeSet);
                LdapConnection conn = new LdapConnection();
                Console.WriteLine("Connecting to:" + ldapHost);
                conn.Connect(ldapHost, ldapPort);
                conn.Bind(loginDN, password);
                conn.Add(newEntry);
                Console.WriteLine("Entry:" + dn + "  Added Successfully");
                conn.Disconnect();