本文整理汇总了C#中X509CertificateCollection.AddRange方法的典型用法代码示例。如果您正苦于以下问题:C# X509CertificateCollection.AddRange方法的具体用法?C# X509CertificateCollection.AddRange怎么用?C# X509CertificateCollection.AddRange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类X509CertificateCollection
的用法示例。
在下文中一共展示了X509CertificateCollection.AddRange方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LoadCertificates
static X509CertificateCollection LoadCertificates (string filename)
{
X509Certificate x509 = null;
X509CertificateCollection coll = new X509CertificateCollection ();
switch (Path.GetExtension (filename).ToUpper ()) {
case ".P7B":
case ".SPC":
SoftwarePublisherCertificate spc = SoftwarePublisherCertificate.CreateFromFile (filename);
coll.AddRange (spc.Certificates);
spc = null;
break;
case ".CER":
case ".CRT":
using (FileStream fs = File.OpenRead (filename)) {
byte[] data = new byte [fs.Length];
fs.Read (data, 0, data.Length);
if (data [0] != 0x30) {
// maybe it's ASCII PEM base64 encoded ?
data = PEM ("CERTIFICATE", data);
}
if (data != null)
x509 = new X509Certificate (data);
}
if (x509 != null)
coll.Add (x509);
break;
case ".P12":
case ".PFX":
// TODO - support PKCS12 with passwords
PKCS12 p12 = PKCS12.LoadFromFile (filename);
coll.AddRange (p12.Certificates);
p12 = null;
break;
default:
Console.WriteLine ("Unknown file extension: {0}",
Path.GetExtension (filename));
break;
}
return coll;
}
示例2: X509CertificateCollectionThrowsArgumentNullException
public static void X509CertificateCollectionThrowsArgumentNullException()
{
using (X509Certificate certificate = new X509Certificate())
{
Assert.Throws<ArgumentNullException>(() => new X509CertificateCollection((X509Certificate[])null));
Assert.Throws<ArgumentNullException>(() => new X509CertificateCollection((X509CertificateCollection)null));
X509CertificateCollection collection = new X509CertificateCollection { certificate };
Assert.Throws<ArgumentNullException>(() => collection[0] = null);
Assert.Throws<ArgumentNullException>(() => collection.Add(null));
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509Certificate[])null));
Assert.Throws<ArgumentNullException>(() => collection.AddRange((X509CertificateCollection)null));
Assert.Throws<ArgumentNullException>(() => collection.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>(() => collection.Insert(0, null));
Assert.Throws<ArgumentNullException>(() => collection.Remove(null));
IList ilist = (IList)collection;
Assert.Throws<ArgumentNullException>(() => ilist[0] = null);
Assert.Throws<ArgumentNullException>(() => ilist.Add(null));
Assert.Throws<ArgumentNullException>(() => ilist.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>(() => ilist.Insert(0, null));
Assert.Throws<ArgumentNullException>(() => ilist.Remove(null));
}
Assert.Throws<ArgumentNullException>(() => new X509CertificateCollection.X509CertificateEnumerator(null));
}
示例3: LoadCertificates
static X509CertificateCollection LoadCertificates (string filename, string password, bool verbose)
{
X509Certificate x509 = null;
X509CertificateCollection coll = new X509CertificateCollection ();
switch (Path.GetExtension (filename).ToUpper ()) {
case ".P7B":
case ".SPC":
SoftwarePublisherCertificate spc = SoftwarePublisherCertificate.CreateFromFile (filename);
coll.AddRange (spc.Certificates);
spc = null;
break;
case ".CER":
case ".CRT":
using (FileStream fs = File.OpenRead (filename)) {
byte[] data = new byte [fs.Length];
fs.Read (data, 0, data.Length);
if (data [0] != 0x30) {
// maybe it's ASCII PEM base64 encoded ?
data = PEM ("CERTIFICATE", data);
}
if (data != null)
x509 = new X509Certificate (data);
}
if (x509 != null)
coll.Add (x509);
break;
case ".P12":
case ".PFX":
PKCS12 p12 = password == null ? PKCS12.LoadFromFile (filename)
: PKCS12.LoadFromFile (filename, password);
X509CertificateCollection tmp = new X509CertificateCollection (p12.Certificates);
for (int i = 0; i != p12.Keys.Count; i++) {
X509Certificate cert = p12.Certificates[i];
RSACryptoServiceProvider pk = p12.Keys[i] as RSACryptoServiceProvider;
if (pk == null || pk.PublicOnly)
continue;
if (verbose)
Console.WriteLine ("Found key for certificate: {0}", cert.SubjectName);
tmp[0].RSA = pk;
}
coll.AddRange(tmp);
p12 = null;
break;
default:
Console.WriteLine ("Unknown file extension: {0}",
Path.GetExtension (filename));
break;
}
return coll;
}