Csharp/C Sharp by API/System.Security.Cryptography.X509Certificates/X509Certificate — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

X509Certificate.CreateFromCertFile

  
using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;
class MainClass {
    public static void Main() {
        WebRequest requestA = WebRequest.Create("http://www.yoursite.ru");
        requestA.Credentials = new NetworkCredential("userName", "password");
        requestA.PreAuthenticate = true;
        WebRequest requestB = WebRequest.Create("http://www.yoursite.ru");
        requestB.Credentials = CredentialCache.DefaultCredentials;
        requestB.PreAuthenticate = true;
        HttpWebRequest requestC = (HttpWebRequest)WebRequest.Create("http://www.yoursite.ru");
        X509Certificate cert1 = X509Certificate.CreateFromCertFile("TestCertificate.cer");
        requestC.ClientCertificates.Add(cert1);
        HttpWebRequest requestD = (HttpWebRequest)WebRequest.Create("http://www.yoursite.ru");
        X509Store store = new X509Store();
        X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectName,"Joe", false);
        if (certs.Count == 1) {
            requestD.ClientCertificates.Add(certs[0]);
        } else {
            certs = X509Certificate2UI.SelectFromCollection(
                store.Certificates,
                "Select Certificate",
                "Select the certificate to use for authentication.",
                X509SelectionFlag.SingleSelection);
            if (certs.Count != 0) {
                requestD.ClientCertificates.Add(certs[0]);
            }
        }
    }
}