Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Sonstiges (https://www.delphipraxis.net/45-library-sonstiges/)
-   -   C# Windows´ Zertifikatsspeicher verwenden (https://www.delphipraxis.net/95953-windows%B4-zertifikatsspeicher-verwenden.html)

TurboMartin 16. Jul 2007 15:37


Windows´ Zertifikatsspeicher verwenden
 
benötigt möglicherweiße .Net 2.0, weiß ich aber nicht genau :?

"Problem":
Zertifikate werden logischer weise von Windows im Zertifikats-Speicher ab. Wenn man Sicherheits-APIs, die auf Zertifikaten basierten (z.B. SSL, WCF oder CAS), nutzen möchte, muss man auf Zertifikate aus dem Speicher zugreifen.

Lösung:
Zuerst muss man "system.security.dll" einbinden.
Daraufhin kann man nun mit folgendem Code auf den Speicher zugreifen:
Code:
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);                                           //Hierbei wird möglicherweise ein neuer Speicher erstellt!
Mögliche Flags für store.open:
Code:
Speicherort:

  Value                               Meaning
  CAPICOM_ACTIVE_DIRECTORY_USER_STORE The store is an Active Directory store. No error will be generated if an
                                       Active Directory store is opened as read/write, but any changes to the store
                                       will not be persisted. Certificates cannot be added to or removed from
                                       Active Directory stores.
  CAPICOM_CURRENT_USER_STORE          The store is a current user store. A current user store may be a read/write
                                       store. If it is, changes in the contents of the store are persisted.
  CAPICOM_LOCAL_MACHINE_STORE         The store is a local computer store. Local computer stores can be read/write
                                       stores only if the user has read/write permissions. If the user has read/write
                                       permissions and if the store is opened read/write, then changes in the
                                       contents of the store are persisted.
  CAPICOM_MEMORY_STORE                The store is a memory store. Any changes in the contents of the store are
                                       not persisted.
  CAPICOM_SMART_CARD_USER_STORE       The store is the group of present smart cards. Introduced in CAPICOM 2.0.


Name des Speichers:

  Value                               Meaning
  CAPICOM_CA_STORE CA store.          This store is used to store intermediate CA certificates.
  CAPICOM_MY_STORE My store.          This store is used for a user's personal certificates.
  CAPICOM_OTHER_STORE                 AddressBook store. This store is used to keep the certificates of others.
  CAPICOM_ROOT_STORE                  Root store. This store is used to store the root CA and self-signed,
                                       trusted certificates.


Methode zum öffnen:

  Value                               Meaning
  CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED  Open the store in read/write mode if the user has read/write permissions;
                                       otherwise, open the store in read-only mode.
  CAPICOM_STORE_OPEN_READ_ONLY        Open the store in read-only mode.
  CAPICOM_STORE_OPEN_READ_WRITE       Open the store in read/write mode.

  nur kombinierbare Flags:

  CAPICOM_STORE_OPEN_EXISTING_ONLY    Open existing stores only; do not create a new store.
                                       NUR AB CAPICOM 2.0. (nur zu empfehlen!)
  CAPICOM_STORE_OPEN_INCLUDE_ARCHIVED Include archived certificates when using the store.
                                       NUR AB CAPICOM 2.0.

Iterieren durch Zertifikate:
Code:
foreach (X509Certificate2 cert in store.Certificates)
{
  Console.WriteLine(cert.Subject);
}

Nun kann außerdem den Zertifikats-Auswahldialog anzeigen (z.B. damit man ein passendes Zertifikat aus dem Speicher auswählen kann):
Code:
X509Certificate2Collection certs =
X509Certificate2UI.SelectFromCollection(store.Certificates,
  "Ihre Zertifikate",                                          //Beschriftung der Titelleiste
  "Bitte auswählen",                                           //möglicher Text, der im Fenter über der Liste
                                                                //angezeigt werden soll
  X509SelectionFlag.SingleSelection);                          //mit "MultiSelection" lassen sich mehrere auswählen

Außerdem lassen sich Details anzeigen zu einen Zertifikat:
Code:
if (certs.Count != 0)
{
  X509Certificate2UI.DisplayCertificate(certs[0]);
}

Zertifikate lassen sich wie folgt suchen:
Code:
X509Certificate2Collection result =
  store.Certificates.Find(
    X509FindType.FindBySubjectKeyIdentifier,
    "abc...",                                    //Suchbegriff
    true);

Zuletzt muss dann der Speicher geschlossen werden:
Code:
store.Close();


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:52 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz