本文整理汇总了C#中X509CertificateCollection.Add方法的典型用法代码示例。如果您正苦于以下问题:C# X509CertificateCollection.Add方法的具体用法?C# X509CertificateCollection.Add怎么用?C# X509CertificateCollection.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类X509CertificateCollection
的用法示例。
在下文中一共展示了X509CertificateCollection.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
string host = "localhost";
if (args.Length > 0)
host = args[0];
SslProtocols protocol = SslProtocols.Tls;
if (args.Length > 1) {
switch (args [1].ToUpper ()) {
case "SSL":
protocol = SslProtocols.Ssl3;
break;
}
}
X509CertificateCollection certificates = null;
if (args.Length > 2) {
string password = null;
if (args.Length > 3)
password = args [3];
p12 = Mono.Security.X509.PKCS12.LoadFromFile(args [2], password);
certificates = new X509CertificateCollection ();
foreach (Mono.Security.X509.X509Certificate cert in p12.Certificates) {
certificates.Add(new X509Certificate2(args [2], password));
break;
}
}
TcpClient client = new TcpClient ();
client.Connect (host, 4433);
SslStream ssl = new SslStream (client.GetStream(), false, new RemoteCertificateValidationCallback (CertificateValidation), new LocalCertificateSelectionCallback (ClientCertificateSelection));
ssl.AuthenticateAsClient (host, certificates, protocol, false);
StreamWriter sw = new StreamWriter (ssl, System.Text.Encoding.ASCII);
sw.WriteLine ("GET /clientcert.aspx{0}", Environment.NewLine);
sw.Flush ();
StreamReader sr = new StreamReader (ssl);
Console.WriteLine (sr.ReadToEnd ());
}
示例2: Main
static void Main(string[] args)
{
string host = "localhost";
if (args.Length > 0)
host = args[0];
SecurityProtocolType protocol = SecurityProtocolType.Tls;
if (args.Length > 1) {
switch (args [1].ToUpper ()) {
case "SSL":
protocol = SecurityProtocolType.Ssl3;
break;
}
}
X509CertificateCollection certificates = null;
if (args.Length > 2) {
string password = null;
if (args.Length > 3)
password = args [3];
p12 = Mono.Security.X509.PKCS12.LoadFromFile(args [2], password);
certificates = new X509CertificateCollection ();
foreach (Mono.Security.X509.X509Certificate cert in p12.Certificates) {
certificates.Add(new X509Certificate(cert.RawData));
}
}
TcpClient client = new TcpClient ();
client.Connect (host, 4433);
SslClientStream ssl = new SslClientStream (client.GetStream(), host, false, protocol, certificates);
ssl.ServerCertValidationDelegate += new CertificateValidationCallback (CertificateValidation);
ssl.ClientCertSelectionDelegate += new CertificateSelectionCallback (ClientCertificateSelection);
ssl.PrivateKeyCertSelectionDelegate += new PrivateKeySelectionCallback (PrivateKeySelection);
StreamWriter sw = new StreamWriter (ssl, System.Text.Encoding.ASCII);
sw.WriteLine ("GET /clientcert.aspx{0}", Environment.NewLine);
sw.Flush ();
StreamReader sr = new StreamReader (ssl);
Console.WriteLine (sr.ReadToEnd ());
}
示例3: 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));
}
示例4: X509CertificateCollectionInsertAndClear
public static void X509CertificateCollectionInsertAndClear()
{
using (X509Certificate c1 = new X509Certificate())
using (X509Certificate c2 = new X509Certificate())
using (X509Certificate c3 = new X509Certificate())
{
X509CertificateCollection cc = new X509CertificateCollection();
cc.Insert(0, c1);
cc.Insert(1, c2);
cc.Insert(2, c3);
Assert.Equal(3, cc.Count);
Assert.Same(c1, cc[0]);
Assert.Same(c2, cc[1]);
Assert.Same(c3, cc[2]);
cc.Clear();
Assert.Equal(0, cc.Count);
cc.Add(c1);
cc.Add(c3);
Assert.Equal(2, cc.Count);
Assert.Same(c1, cc[0]);
Assert.Same(c3, cc[1]);
cc.Insert(1, c2);
Assert.Equal(3, cc.Count);
Assert.Same(c1, cc[0]);
Assert.Same(c2, cc[1]);
Assert.Same(c3, cc[2]);
cc.Clear();
Assert.Equal(0, cc.Count);
IList il = cc;
il.Insert(0, c1);
il.Insert(1, c2);
il.Insert(2, c3);
Assert.Equal(3, il.Count);
Assert.Same(c1, il[0]);
Assert.Same(c2, il[1]);
Assert.Same(c3, il[2]);
il.Clear();
Assert.Equal(0, il.Count);
il.Add(c1);
il.Add(c3);
Assert.Equal(2, il.Count);
Assert.Same(c1, il[0]);
Assert.Same(c3, il[1]);
il.Insert(1, c2);
Assert.Equal(3, il.Count);
Assert.Same(c1, il[0]);
Assert.Same(c2, il[1]);
Assert.Same(c3, il[2]);
il.Clear();
Assert.Equal(0, il.Count);
}
}
示例5: GetCertificates
protected virtual X509CertificateCollection GetCertificates ()
{
var certificates = new X509CertificateCollection ();
var certificate = Config.Certificate;
if (certificate == null)
return certificates;
var verifyVertificate = true;
#if INSTRUMENTATION
if (Context.HasInstrument (HandshakeInstrumentType.OverrideClientCertificateSelection))
verifyVertificate = false;
#endif
var exchangeAlgorithm = PendingCrypto.Cipher.ExchangeAlgorithmType;
if (verifyVertificate && !CertificateManager.VerifyClientCertificate (Context, certificate, exchangeAlgorithm))
throw new TlsException (AlertDescription.UnsupportedCertificate);
certificates.Add (certificate);
return certificates;
}
示例6: ValidateClientCertificate
internal bool ValidateClientCertificate (X509Certificate certificate, MonoSslPolicyErrors errors)
{
var certs = new X509CertificateCollection ();
certs.Add (new X509Certificate2 (certificate.GetRawCertData ()));
var result = ValidateChain (string.Empty, true, certificate, null, certs, (SslPolicyErrors)errors);
if (result == null)
return false;
return result.Trusted && !result.UserDenied;
}
示例7: X509CertificateCollectionAsIListBogusEntry
public static void X509CertificateCollectionAsIListBogusEntry()
{
using (X509Certificate2 c = new X509Certificate2())
{
IList il = new X509CertificateCollection();
il.Add(c);
string bogus = "Bogus";
Assert.Throws<ArgumentException>(() => il[0] = bogus);
Assert.Throws<ArgumentException>(() => il.Add(bogus));
Assert.Throws<ArgumentException>(() => il.Remove(bogus));
Assert.Throws<ArgumentException>(() => il.Insert(0, bogus));
}
}
示例8: X509CertificateCollectionAsIList
public static void X509CertificateCollectionAsIList()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2())
{
IList il = new X509CertificateCollection();
il.Add(c1);
il.Add(c2);
Assert.Throws<ArgumentNullException>(() => il[0] = null);
string bogus = "Bogus";
Assert.Throws<ArgumentException>(() => il[0] = bogus);
Assert.Throws<ArgumentException>(() => il.Add(bogus));
Assert.Throws<ArgumentException>(() => il.Insert(0, bogus));
}
}
示例9: ProcessAsTls1
protected override void ProcessAsTls1()
{
this.certificates = new X509CertificateCollection();
int readed = 0;
int length = this.ReadInt24();
while (readed < length)
{
// Read certificate length
int certLength = ReadInt24();
// Increment readed
readed += 3;
if (certLength > 0)
{
// Read certificate data
byte[] buffer = this.ReadBytes(certLength);
// Create a new X509 Certificate
X509Certificate certificate = new X509Certificate(buffer);
certificates.Add(certificate);
readed += certLength;
DebugHelper.WriteLine(
String.Format("Server Certificate {0}", certificates.Count),
buffer);
}
}
this.validateCertificates(certificates);
}
示例10: iOSAPNS
/*
public string iOSAPNS(String DeviceTokenlistString, String content)
{
APNSSender test = new APNSSender();
return test.SendAPNS(DeviceTokenlistString, content);
}
*/
/// <summary>
/// 傳入 DeviceToken 與 要傳的訊息
/// </summary>
/// 手機上方 最多五則
/// 警報日期:2015/03/04 11:55:00 \n類型/嚴重度:超速 / [嚴重]\n名稱:連續超速70km 3分鐘\n車號/駕駛人:kw-car03 / 易大師
/// APNS 限制 payload 256bytes 超過就不發了 不过由于aps,alert等会占用部分长度,所以一般剩余140-200之间。
/// 實測560個字都傳的出去 只是手機顯示最多4行而已
/// <param name="inDeviceToken ID"> 手機端 Registration ID</param>
/// <param name="inMessage">訊息內容</param>
public String SendAPNS(String inDeviceToken, String inMessage)
{
//http://blog.sina.com.cn/s/blog_6f9a9718010128hi.html
//太連續用單一個發送 會被蘋果認為是 dos 攻擊 而中斷連線 用單一個多次發送 遇到錯誤的token會中斷發送 接下來的都會無法發送
System.Threading.Thread.Sleep(50);
// str = "{\"aps\":{\"alert\":\"" + s2 + "\",\"badge\":10,\"sound\":\"default\"}}";
// badge 設為 0 就不會出現數字
mAPNSMessage = "{\"aps\":{\"alert\":\"" + inMessage + "\",\"badge\":0,\"sound\":\"default\"}}";
int port = 2195;
// string certificatepath = ConfigurationManager.AppSettings["p12FilePath"];
// string certificatepath = "D:\\VisualStudioProject\\Fleet\\new_aps_developer.p12";
//string certificatepath = "D:\\VisualStudioProject\\Fleet\\distribution_aps_developer.p12";
// string certificatepath = "D:\\VisualStudioProject\\Fleet\\fleetivity_aps_developer.p12";
// string certificatepath = "C:\\inetpub\\DotNetNuke\\fleetivity_aps_developer.p12";
string certificatepath = HttpContext.Current.Server.MapPath("/") + "certificate\\fleetivity_aps_developer.p12";
string password = "abc123";
// Apple development server address
string hostIP = "gateway.sandbox.push.apple.com";//
string p12Filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, certificatepath);
certificate = new X509Certificate2(System.IO.File.ReadAllBytes(p12Filename), password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
certificates = new X509CertificateCollection();
certificates.Add(certificate);
if (certificate.ToString().Contains("Production"))
{
hostIP = "gateway.push.apple.com";
}
else
{
hostIP = "gateway.sandbox.push.apple.com";
}
TcpClient apnsClient = new TcpClient();
apnsClient.Connect(hostIP, port);
SslStream apnsStream = new SslStream(apnsClient.GetStream(), false, new RemoteCertificateValidationCallback(validateServerCertificate), new LocalCertificateSelectionCallback(selectLocalCertificate));
try
{
apnsStream.AuthenticateAsClient(hostIP, certificates, System.Security.Authentication.SslProtocols.Tls, false);
}
catch (System.Security.Authentication.AuthenticationException ex)
{
Console.WriteLine("error:" + ex.Message);
}
if (!apnsStream.IsMutuallyAuthenticated)
{
Console.WriteLine("error:" + "Ssl Stream Failed to Authenticate");
}
if (!apnsStream.CanWrite)
{
Console.WriteLine("error:" + "Ssl Stream is not Writable");
}
Byte[] message = ToBytes(inDeviceToken);
apnsStream.Write(message);
return System.Text.Encoding.UTF8.GetString(message);
}
示例11: 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;
}
示例12: BuildCertificatesCollection
private X509CertificateCollection BuildCertificatesCollection (string storeName)
{
X509CertificateCollection coll = new X509CertificateCollection ();
string path = Path.Combine (_storePath, storeName);
if (!CheckStore (path, false))
return coll; // empty collection
string[] files = Directory.GetFiles (path, "*.cer");
if ((files != null) && (files.Length > 0)) {
foreach (string file in files) {
try {
X509Certificate cert = LoadCertificate (file);
coll.Add (cert);
}
catch {
// in case someone is dumb enough
// (like me) to include a base64
// encoded certs (or other junk
// into the store).
}
}
}
return coll;
}
示例13: GetCertificates
protected virtual X509CertificateCollection GetCertificates ()
{
var certificates = new X509CertificateCollection ();
if (Config.Certificate != null)
certificates.Add (Config.Certificate);
return certificates;
}
示例14: X509CertificateCollectionEnumeratorModification
public static void X509CertificateCollectionEnumeratorModification()
{
using (X509Certificate c1 = new X509Certificate())
using (X509Certificate c2 = new X509Certificate())
using (X509Certificate c3 = new X509Certificate())
{
X509CertificateCollection cc = new X509CertificateCollection(new X509Certificate[] { c1, c2, c3 });
X509CertificateCollection.X509CertificateEnumerator e = cc.GetEnumerator();
cc.Add(c1);
// Collection changed.
Assert.Throws<InvalidOperationException>(() => e.MoveNext());
Assert.Throws<InvalidOperationException>(() => e.Reset());
}
}
示例15: X509CertificateCollectionAsIList
public static void X509CertificateCollectionAsIList()
{
using (X509Certificate2 c1 = new X509Certificate2())
using (X509Certificate2 c2 = new X509Certificate2())
{
X509CertificateCollection cc = new X509CertificateCollection();
cc.Add(c1);
cc.Add(c2);
IList il = cc;
Assert.Throws<ArgumentNullException>(() => il[0] = null);
il[0] = "Bogus";
Assert.Equal("Bogus", il[0]);
object ignored;
Assert.Throws<InvalidCastException>(() => ignored = cc[0]);
}
}